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) {
if(Trigger.isBefore){
if(Trigger.isInsert){
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>();
testSurveys.add(testSurvey);
createRelationships.CreateAccountRelationship(testSurveys);
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?