Guest User

Untitled

a guest
Oct 16th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. public class ServiceRequestTrigger {
  2. private final Map<Id, Service_Request__c> oldMap;
  3. private final Map<Id, Service_Request__c> newMap;
  4. private final List<Service_Request__c> newObjs;
  5. private final Boolean isInsert;
  6. private final Boolean isUpdate;
  7. private final Boolean isDelete;
  8. private final Boolean isBulk;
  9.  
  10. /**
  11. * The constructor
  12. *
  13. * @param xoldMap The old map from the trigger
  14. * @param xnewObj The list of new objects from the trigger
  15. * @param isBefore If the trigger is in before or after
  16. */
  17. public ServiceRequestTrigger(Map<Id, Service_Request__c> xoldMap, List<Service_Request__c> xnewObjs, Boolean isBefore) {
  18. oldMap = xoldMap;
  19. newObjs = xnewObjs;
  20.  
  21. if (!isBefore && newObjs != null) {
  22. newMap = new Map<Id, Service_Request__c>(newObjs);
  23. }
  24.  
  25. isDelete = (((newObjs == null || newObjs.isEmpty()) && isBefore) || ((newMap == null || newMap.isEmpty()) && !isBefore));
  26. isUpdate = ! (isDelete || oldMap == null || oldMap.isEmpty());
  27. isInsert = ! (isDelete || isUpdate);
  28. isBulk = (newObjs != null && newObjs.size() > 1) ? true : false;
  29. }
  30.  
  31. public void doAwesomeness() {
  32. set<Id> objIds = new Set<Id>();
  33.  
  34. for (Service_Request__c srf: newObjs) {
  35. if (
  36. oldMap.get(srf.ID) != null &&
  37. oldMap.get(srf.ID).Status__c != srf.Status__c &&
  38. srf.Status__c == 'In Review'
  39. ) {
  40. //build up the list/map in here
  41. objIds.add(srf.Id);
  42. }
  43.  
  44. List <Service_Request__c> srfList =
  45. [SELECT Status__c, Id
  46. FROM Service_Request__c
  47. WHERE Status__c = 'In Review'
  48. AND Id IN: objIds];
  49. }
  50.  
  51. List<Vendor_Scorecard__c> vscList = new List<Vendor_Scorecard__c>();
  52.  
  53. //do the logic and processing here
  54. for (Service_Request__c srf: srfList){
  55. Vendor_Scorecard__c vsc = new Vendor_Scorecard__c (vsc.Service_Request__c = srf.Id);
  56. vscList.add(vsc);
  57. }
  58. if (!vscList.isEmpty()) {
  59. try {
  60. insert vscList;
  61. } catch (DmlException e) {
  62. ApexPages.addMessages(e);
  63. return null;
  64. }
  65. }
  66. }
  67. /**
  68. * Method to initiate trigger logic
  69. *
  70. * @param oldMap The old map from the trigger
  71. * @param newObj The list of new objects from the trigger
  72. * @param isBefore If the trigger is in before or after
  73. */
  74. public static void processTrigger(Map<Id, Service_Request__c> oldMap, List<Service_Request__c> newObj, Boolean isBefore) {
  75. final ServiceRequestTrigger myTrigger = new ServiceRequestTrigger(oldMap, newObj, isBefore);
  76.  
  77. if (!isBefore) {
  78. myTrigger.doAwesomeness();
  79. }
  80. }
  81. }
Add Comment
Please, Sign In to add comment