You need to sign in to do that
Don't have an account?

Help on efficiency for this trigger
I basically copied the functionality for the original trigger (maps an external id field from one record, to another external id field, and changes the lookup value to that - this is for Salesforce to Salesforce)
I added AccountIds modelled after CountyIds - i just added another set of queries/fields/maps etc. Is there a better way to do this other than what i have below?
trigger AddCountyId on Contact (before insert, before update) { //Create a unique list of Counties Set<String> CountyIds = new Set<String>(); Set<String> AccountIds = new Set<String>(); for(Contact c : Trigger.new) { CountyIds.add(c.fdicCountyID__c); AccountIds.add(c.fdicAccountID__c); } //Create a Map to match up Contact FDIC County Id and FDIC County Id Map<String, Id> countyMap = new Map<String,Id>(); Map<String, Id> accountMap = new Map<String,Id>(); //Add both sets of IDS to above Map for(County__c cty : [SELECT cty.Id, cty.fdicCountyID__c FROM County__c cty WHERE cty.fdicCountyID__c != null AND cty.fdicCountyID__c IN :CountyIds]) { countyMap.put(cty.fdicCountyID__c, cty.Id); } for(Account acct : [SELECT acct.Id, acct.fdicAccountID__c FROM Account acct WHERE acct.fdicAccountID__c != null AND acct.fdicAccountID__c IN :AccountIds]) { accountMap.put(acct.fdicAccountID__c, acct.Id); } //Update Primary County on the Contact with County ID for(Contact updateC : Trigger.new) { try { if(updateC.fdicCountyID__c != null || updateC.fdicAccountID__c != null) { //Update Primary County with County Id from Map updateC.Primary_County__c = countyMap.get(updateC.fdicCountyID__c); updateC.AccountId = accountMap.get(updateC.fdicAccountID__c); } } catch (Exception e) { } } }
If you canould do this in an after insert then you could cut out the scripts to create the sets....
Is it possible to use an after insert vs before insert for your use case?
You will have to create a class with a static variable called canIrun or whatever you choose (just update above) to set to false when you do the update so you do not cause recursion. This will allow it to handle bulk updates which you could not do before
I'm still pretty new to apex and triggers - I didnt know that you can call a variable in a class like that in a trigger - Our org has a contactExtension class - I'll tweak it and this trigger with your suggestions and let you know how it goes - thanks!
I get the following error: semi join sub selects an only query id fields, cannot use: 'fdicCountyId__c' on line 10.
fdicCountyId__c is actually a text field.
Ok, so it is not an ID, then you will still have to use the sets but can incorporate the single update as follows:
The static variable class would be something like:
public class StaticVariables{
Public Static Boolean canIRun = true;
}
I really appreciate all the help for this - but now there is another error:
AddCountyId: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.AddCountyId: line 37, column 1
where the code is below:
I guess if I would have thought about the logic of the trigger I would have realized you were updating the records that are in the trigger.....
OK, Looks like for the most part your original trigger works best....
I actually brain-farted too - because in an after insert/after update - you wouldn't be able to update records. I dont know what I was thinking...
I guess this just goes back to the original question - is there a way to basically cut out some of the doubles in code i've created to account for a different object (which needs the same behavior in the trigger).