Our company deploys surveys from an external system. We receive them in Salesforce, but we don't have any relationships. I thought I could create the relationship with an External Ids in a Before Insert trigger, but it didn't work:

trigger SurveryTrigger on Survey__c (before insert) {
            for(Survey__c survey : Trigger.new){
                Account account = new Account();
                account.Foreign_key__c = survey.Account_key__c;
                survey.Account__r = account;

Furthermore, I decided to refactor this into a class and it worked fine when I executed anonymously:

SurveyCreateRelationships createRelationships = new SurveyCreateRelationships();

Survey__c testSurvey = new Survey__c();
testSurvey.Account_key__c = 'xxxxxx';

List<Survey__c> testSurveys = new List<Survey__c>();


insert testSurveys;

The only thing I can figure is that the before insert doesn't support creating this type of relationship. Has anyone had any similar trouble?

1 Answer 1


Old code contained a few errors. Here is the Trigger, and test code. This passed the apex test method in my org.

Apex Trigger
trigger surveyTrigger on Survey__c (before insert) { if(Trigger.isBefore){ if(Trigger.isInsert){ Map<String, String> extMap = new Map<String, String>(); Set<String> extIdSet = new Set<String>(); for(Survey__c survey : Trigger.new){ extIdSet.add(survey.Account_Key__c); } for(Account a : [select Id, Foreign_Key__c from Account where Foreign_Key__c IN :extIdSet]){ extMap.put(a.Foreign_Key__c, a.Id); } for(Survey__c survey : Trigger.new){ survey.Account__c = extMap.get(survey.Account_Key__c); } } } }

Test class @isTest private class TestSurveyTrigger { static testMethod void myUnitTest() { Account testAccount = new Account(Name='My test account', Foreign_Key__c='test123'); insert testAccount; Test.startTest(); Survey__c testSurvey = new Survey__c(Account_Key__c='test123'); insert testSurvey; Test.stopTest(); Survey__c result = [select Id, Account__c, Account__r.Name, Account__r.Foreign_Key__c from Survey__c where Account_Key__c = 'test123' limit 1]; system.assertEquals(testAccount.Id, result.Account__c); } }

  • 1
    Sorry if I wasn't very clear, but the main issue isn't the implementation (although I will give your first approach a try). It seems that Salesforce's external Id matching field doesn't create the relationship when it's run from the Before Insert trigger, but will work when run manually. I can verify that the foreign key is correctly setup as an external Id, but I'm not very clear on what is happening behind the scenes with triggers. I assumed it would work by simply running my logic and then inserting the records. Maybe this functionality isn't supported? May 6, 2014 at 18:29
  • Nice, gave this a shot and it worked! Still puzzled why 'survey.Account__r = new Account(Foreign_key__c=survey.Account_Id__c);' wasn't working. Thanks for the help! May 8, 2014 at 20:29
  • 1
    This is correct, In Before Insert building lookup relationship via External ID does not work and creating map of String and ID with 1 SOQL will do the required job as mentioned by Philipp Rackwitz above. Aug 29, 2018 at 19:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .