Advertisement
Guest User

OppRecursion

a guest
Jul 17th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. trigger OppRecursion on Opportunity (after update) {
  2.  
  3. // Create empty opportunity list
  4. List<Opportunity> oppsToUpdate = new List<Opportunity>();
  5.  
  6. //Create empty set
  7. Set<Id> oppIds = new Set<Id>();
  8.  
  9. // Iterate through selected opp records
  10. for (Opportunity opp : Trigger.new){
  11. oppIds.add(opp.id);
  12.  
  13. }
  14.  
  15. // Fetch selected records
  16. List<Opportunity> opportunityList = [select Id, Description from Opportunity where Id in:oppIds];
  17.  
  18. for (Opportunity opportunityRecord:opportunityList){
  19. //The description will always be updated to Test every time the record is updated
  20. //This causes recursion because updating the field triggers the after update trigger
  21. //How can we stop the recursion?
  22. opportunityRecord.Description = 'Test';
  23. oppsToUpdate.add(opportunityRecord);
  24.  
  25. }
  26.  
  27. if (!oppsToUpdate.isEmpty()){
  28. update oppsToUpdate;
  29. }
  30.  
  31.  
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement