function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Marmot74Marmot74 

Help with Bulk Trigger

Hey Guys,

 

I am new to this. What is wrong with the trigger below? is the logic totally out of wack? I updates the AccountID field on Contact based on a query from the Account table using an ExternalID field. No errors, it just doesn't update the AccountID.

 

trigger OnEFO_CreateContact on Contact (before insert, before update) { Set<String> conOrg_IDs = new Set<String>(); for(Contact Con : trigger.new){ if(Con.Org_ID__c != null && Con.AccountID == null){ conOrg_IDs.add(Con.Org_ID__c); } } Map<String,Account> AccountMap = new Map<String,Account>([SELECT Org_ID__c, ID FROM Account where Org_ID__c IN : conOrg_IDs]); for(Contact Con : trigger.new){ if(AccountMap.containsKey(Con.Org_ID__c)){ Con.AccountID = AccountMap.get(Con.Org_ID__c).ID; } } }

 

JimRaeJimRae

My guess is that your map is not getting created the way you want it to.  Using the syntax you are using, the ID of the map will be the id of the object being queried.

 

You probably want it like this:

 

 

trigger OnEFO_CreateContact on Contact (before insert, before update) { Set<String> conOrg_IDs = new Set<String>(); for(Contact Con : trigger.new){ if(Con.Org_ID__c != null && Con.AccountID == null){ conOrg_IDs.add(Con.Org_ID__c); } } Map<String,Account> AccountMap = new Map<String,Account>(); for(Account a :[SELECT Org_ID__c, ID FROM Account where Org_ID__c IN : conOrg_IDs]){ AccountMap.put(a.Org_ID__C,a); } for(Contact Con : trigger.new){ if(AccountMap.containsKey(Con.Org_ID__c)){ Con.AccountID = AccountMap.get(Con.Org_ID__c).ID; } } }