Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. static map<id,id> beforeUpdateMap;
  2.  
  3. public static void handleBeforeUpdate( list<Account> newAccounts){
  4.  
  5. set<id> actIds = new set<id>();
  6. for(Account a : newAccounts){
  7. actIds.add(a.id);
  8. }
  9. list<Opportunity> opps = [Select id, accountid, ownerid, owner.name from Opportunity where accountid in :actids];
  10. beforeUpdateMap=new map<id,id>();
  11. for(Opportunity o : opps){
  12. system.debug('***Opp owner on Account beforeUpdate:'+o.owner.name);
  13. beforeUpdateMap.put(o.accountid,o.ownerid);
  14. }
  15. }
  16.  
  17. public static void handleAfterUpdate( list<Account> newAccounts){
  18.  
  19. set<id> actIds = new set<id>();
  20. for(Account a : newAccounts){
  21. actIds.add(a.id);
  22. }
  23. list<Opportunity> updateOpps = new list<Opportunity>();
  24. list<Opportunity> opps = [Select id, isClosed, accountid, ownerid, owner.name from Opportunity where accountid in :actids];
  25. for(Opportunity o : opps){
  26. system.debug('***Opp old owner: '+beforeUpdateMap.get(o.accountid));
  27. system.debug('***Opp owner on Account AfterUpdate:'+o.ownerid);
  28. if(o.isClosed && o.ownerid != beforeUpdateMap.get(o.accountid) ){
  29. o.ownerid=beforeUpdateMap.get(o.accountid);
  30. updateOpps.add(o);
  31. }
  32. }
  33. //SET A TRIGGER FLAG to prevent ANY and ALL triggers from firing on the Opportunity Update
  34. //id triggerFlags.ByPassOpptyTrigger=True
  35. // and in the Opportunity Trigger, always check this flag.
  36. update updateOpps;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement