Guest User

Untitled

a guest
Apr 26th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. public with sharing class CampaignHandler {
  2.  
  3. public static void manageEBCourse(List<Campaign> newList, Map<id, Campaign> oldMap)
  4. {
  5. Set<String> EBIDSet = new Set<String>();
  6. Map<String, Course__c> EBCourseMap = new Map<String, Course__c>();
  7. List<Course__c> courseList = new List<Course__c>();
  8. Map<String, Opportunity> oppMap = new Map<String, Opportunity>();
  9.  
  10. List<Account> vtAccounts = [Select Id, Name from Account where Name = 'VitalTalk' limit 1];
  11. Account vtAccount = null;
  12.  
  13. if (vtAccounts.isEmpty())
  14. {
  15. vtAccount = new Account();
  16. vtAccount.Name = 'VitalTalk';
  17. insert vtAccount;
  18. }
  19. else
  20. vtAccount = vtAccounts[0];
  21.  
  22. if (vtAccount != null)
  23. {
  24. for (Campaign c : newList)
  25. {
  26. if (c.EventbriteSync__EventbriteId__c != null && (Trigger.isInsert || oldMap.get(c.Id).EventbriteSync__EventbriteId__c != c.EventbriteSync__EventbriteId__c || oldMap.get(c.Id).StartDate != c.StartDate || oldMap.get(c.Id).EndDate != c.EndDate))
  27. {
  28. EBIDSet.add(c.EventbriteSync__EventbriteId__c);
  29. }
  30. }
  31.  
  32. if (!EBIDSet.isEmpty())
  33. {
  34. for (Course__c course : [Select Id, EventBrite_ID__c, Opportunity__c from Course__c where EventBrite_ID__c in :EBIDSet])
  35. {
  36. EBCourseMap.put(course.EventBrite_ID__c, course);
  37. }
  38.  
  39. for (Campaign c : newList)
  40. {
  41. if (c.EventbriteSync__EventbriteId__c != null && (Trigger.isInsert || oldMap.get(c.Id).EventbriteSync__EventbriteId__c != c.EventbriteSync__EventbriteId__c || oldMap.get(c.Id).StartDate != c.StartDate || oldMap.get(c.Id).EndDate != c.EndDate))
  42. {
  43. Course__c existingCourse = EBCourseMap.get(c.EventbriteSync__EventbriteId__c);
  44. if (existingCourse == null)
  45. {
  46. Course__c course = new Course__c();
  47. course.Name = c.Name;
  48. course.EventBrite_ID__c = c.EventbriteSync__EventbriteId__c;
  49. course.Start_Date__c = c.StartDate;
  50. course.End_Date__c = c.EndDate;
  51. courseList.add(course);
  52.  
  53. Opportunity o = new Opportunity();
  54. o.Name = c.Name;
  55. o.AccountId = vtAccount.Id;
  56. o.CloseDate = System.today();
  57. o.StageName = 'Closed Won';
  58. o.EventBrite_Event_ID__c = c.EventbriteSync__EventbriteId__c;
  59. oppMap.put(o.EventBrite_Event_ID__c, o);
  60. }
  61. else
  62. {
  63. System.debug('@@@Found course: ' + existingCourse);
  64. Course__c course = new Course__c();
  65. course.Id = existingCourse.Id;
  66. course.Name = c.Name;
  67. course.Start_Date__c = c.StartDate;
  68. course.End_Date__c = c.EndDate;
  69. courseList.add(course);
  70.  
  71. if (existingCourse.Opportunity__c == null)
  72. {
  73. Opportunity o = new Opportunity();
  74. o.Name = c.Name;
  75. o.AccountId = vtAccount.Id;
  76. o.CloseDate = System.today();
  77. o.StageName = 'Closed Won';
  78. o.EventBrite_Event_ID__c = c.EventbriteSync__EventbriteId__c;
  79. oppMap.put(o.EventBrite_Event_ID__c, o);
  80. }
  81. }
  82. }
  83. }
  84.  
  85. if (!oppMap.isEmpty())
  86. upsert oppMap.values();
  87.  
  88. for (Course__c course : courseList)
  89. {
  90. Opportunity o = oppMap.get(course.EventBrite_ID__c);
  91. if (course != null && o != null)
  92. course.Opportunity__c = o.Id;
  93. }
  94.  
  95. if (!courseList.isEmpty())
  96. upsert courseList;
  97. }
  98. }
  99. }
  100.  
  101. }
Add Comment
Please, Sign In to add comment