Last active
September 27, 2019 14:59
-
-
Save mhamzas/f7969628dead16a7b76562388ec77307 to your computer and use it in GitHub Desktop.
General Trigger Pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trigger someTrigger on MyObject__c (before|after insert|update|delete){ | |
// Declare a collection to hold values we want to query against | |
Set<Id> myIdsSet = new Set<Id>(); | |
// Iterate over a collection of objects, and gather the values we want to query | |
// against | |
for(MyObject__c myObj :trigger.new){ | |
myIdsSet.add(myObj.someLookup__c); | |
} | |
// Declare a collection to hold records that we want to update | |
List<OtherObject__c> otherObjsToUpdate = new List<OtherObject__c>(); | |
// Perform the main query you want to run, using the data gathered earlier | |
// to filter the query | |
for(OtherObject__c otherRec :[SELECT <fields> FROM OtherObject__c WHERE Id IN :myIdsSet]){ | |
// The main parts of your trigger logic generally go in here | |
// After you're done with the main logic, add the record to be updated | |
otherObjsToUpdate.add(otherRec); | |
} | |
// Perform the final DML to update the records we worked on so the results are persisted | |
update otherObjsToUpdate; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment