Advertisement
Guest User

Untitled

a guest
Jun 29th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. // This trigger fires on newly inserted or reparented Tasks associated with conortunities,
  2. // and stores the conty's current functional role value in the contact functional role field
  3. trigger updatefunctionalrole1 on Task (before insert, before update) {
  4. // Create a map between the contact ID and its functional role value
  5. Map<ID,String> confunrole1 = new Map<ID,String>();
  6. List<Task> conTasks = new List<Task>();
  7.  
  8. // Loop through the triggered Tasks and add all of the contact IDs (for those associated with conortunities)
  9. for (Task t : trigger.new) {
  10. // Only Tasks associated with conortunities
  11. if (t.whoID!= null && (String.valueOf(t.whoID)).startsWith('003')) {
  12. // And only newly inserted Tasks or those being reparented to an contact
  13. if (trigger.isInsert || (trigger.isUpdate && t.WhoID != trigger.oldMap.get(t.id).WhoID)) {
  14. confunrole1.put(t.whoID,'');
  15. conTasks.add(t);
  16. }
  17. }
  18. }
  19. // Query the conortunities and add their functional role to the map
  20. for (contact con : [SELECT Functional_Role__c FROM contact WHERE ID IN :confunrole1.keySet()]) {
  21. confunrole1.put(con.id,con.Functional_Role__c);
  22. }
  23. // Update the contact functional role field on the Task with the relevant value
  24. for (Task t : trigger.new) {
  25. t.functional_role__c = confunrole1.get(t.whoID);
  26. }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement