fork download
  1. global class ProjectISRAssociation_Batch implements Database.Batchable<Sobject>, Database.Stateful {
  2.  
  3. String query = '';
  4. Set<Id> projIds = new Set<Id>();
  5. List<OpportunityTeamMember> newOptyMembers_for_opt_upd = new List<OpportunityTeamMember>();
  6.  
  7. global ProjectISRAssociation_Batch(List<OpportunityTeamMember> newOptyMembers) {
  8. this.newOptyMembers_for_opt_upd = newOptyMembers;
  9.  
  10. for(OpportunityTeamMember otmTemp : newOptyMembers_for_opt_upd) {
  11. projIds.add(otmTemp.OpportunityId);
  12. }
  13.  
  14. query = 'SELECT Id, Name, Sales_Rep__c, Sales_Rep__r.isActive, EPM__c, EPM__r.isActive, ' +
  15. 'Inside_Sales_Rep__c, Zone_Manager__c, Regional_Sales_Manager__c, OwnerId, Owner.isActive, ' +
  16. 'Owner.Account_Team_Role__c, Owner.Ag__c, ' +
  17. '(SELECT Id, UserId, User.Account_Team_Role__c, User.isActive, TeamMemberRole FROM OpportunityTeamMembers ORDER BY CreatedDate DESC), ' +
  18. '(SELECT Id FROM Engineering_Requests__r) FROM Opportunity WHERE Id IN: projIds';
  19. }
  20.  
  21. global Database.QueryLocator start(Database.BatchableContext BC) {
  22. return Database.getQueryLocator(query);
  23. }
  24.  
  25. global void execute(Database.BatchableContext BC, List<Opportunity> opt_tm_ls) {
  26. List<Opportunity> opptyList_upd = new List<Opportunity>();
  27. Map<Id, Case> casesUpdate = new Map<Id, Case>();
  28.  
  29. try {
  30. // Loop through Opportunities
  31. for(Opportunity opt : opt_tm_ls) {
  32. Boolean isAgRep = opt.Owner.Ag__c;
  33. Map<String, Integer> stusCnt = new Map<String, Integer>();
  34.  
  35. // Count the number of Team Members per role
  36. for(OpportunityTeamMember otmTemp : opt.OpportunityTeamMembers) {
  37. stusCnt.put(otmTemp.TeamMemberRole, stusCnt.getOrDefault(otmTemp.TeamMemberRole, 0) + 1);
  38. }
  39.  
  40. // Process each OpportunityTeamMember
  41. for(OpportunityTeamMember otmTemp : opt.OpportunityTeamMembers) {
  42. processTeamMemberRole(opt, otmTemp, stusCnt, isAgRep, casesUpdate);
  43. }
  44.  
  45. // Add updated Opportunity to the list for bulk update
  46. opptyList_upd.add(opt);
  47. }
  48.  
  49. // Perform DML operations outside of loops
  50. if (!opptyList_upd.isEmpty()) {
  51. update opptyList_upd;
  52. }
  53.  
  54. if (!casesUpdate.isEmpty()) {
  55. update casesUpdate.values();
  56. }
  57. } catch (Exception e) {
  58. System.debug(LoggingLevel.ERROR, 'Error in batch execute: ' + e.getMessage());
  59. }
  60. }
  61.  
  62. global void finish(Database.BatchableContext BC) {
  63. // Any cleanup if needed after batch completion can be added here
  64. }
  65.  
  66. // Helper method to handle the processing of each team member's role
  67. private void processTeamMemberRole(Opportunity opt, OpportunityTeamMember otmTemp,
  68. Map<String, Integer> stusCnt, Boolean isAgRep,
  69. Map<Id, Case> casesUpdate) {
  70. String role = otmTemp.TeamMemberRole;
  71.  
  72. switch on role {
  73. when Constants.ISR_STATUS {
  74. opt.Inside_Sales_Rep__c = otmTemp.UserId;
  75. }
  76. when Constants.ZM_STATUS {
  77. opt.Zone_Manager__c = otmTemp.UserId;
  78. }
  79. when Constants.RSM_STATUS {
  80. opt.Regional_Sales_Manager__c = otmTemp.UserId;
  81. }
  82. when Constants.APM_MANAGER_STATUS {
  83. opt.APM_Manager__c = otmTemp.UserId;
  84. }
  85. when Constants.EPM_MANAGER_STATUS {
  86. opt.EPM_Manager__c = otmTemp.UserId;
  87. }
  88. when Constants.AG_REP_STATUS {
  89. if (isAgRep) {
  90. updateSalesRep(opt, otmTemp, stusCnt, Constants.AG_REP_STATUS, casesUpdate);
  91. }
  92. }
  93. when Constants.SR_STATUS {
  94. if (!isAgRep) {
  95. updateSalesRep(opt, otmTemp, stusCnt, Constants.SR_STATUS, casesUpdate);
  96. }
  97. }
  98. when Constants.MA_STATUS {
  99. opt.Market_Analyst__c = otmTemp.UserId;
  100. }
  101. when Constants.EPM_STATUS {
  102. updateEPM(opt, otmTemp, stusCnt, casesUpdate);
  103. }
  104. when Constants.APM_STATUS {
  105. opt.APM__c = otmTemp.UserId;
  106. updateCases(opt, otmTemp, casesUpdate);
  107. }
  108. when Constants.IPM_STATUS {
  109. opt.IPM__c = otmTemp.UserId;
  110. updateCases(opt, otmTemp, casesUpdate);
  111. }
  112. when else {
  113. // Handle other roles if needed
  114. }
  115. }
  116.  
  117. // Special logic to reassign Opportunity Owner if Account_Team_Role matches and Owner is inactive
  118. if (opt.Owner.Account_Team_Role__c == otmTemp.User.Account_Team_Role__c && !opt.Owner.isActive) {
  119. opt.OwnerId = otmTemp.UserId;
  120. }
  121. }
  122.  
  123. // Helper method to update Sales Rep and Engineering Cases
  124. private void updateSalesRep(Opportunity opt, OpportunityTeamMember otmTemp,
  125. Map<String, Integer> stusCnt, String role, Map<Id, Case> casesUpdate) {
  126. if (stusCnt.get(role) == 1) {
  127. opt.Sales_Rep__c = otmTemp.UserId;
  128. } else if (stusCnt.get(role) > 1) {
  129. if ((opt.Sales_Rep__c == null || !opt.Sales_Rep__r.isActive) && opt.Sales_Rep__c != otmTemp.UserId) {
  130. opt.Sales_Rep__c = otmTemp.UserId;
  131. }
  132. }
  133.  
  134. // Update related Cases with new Sales Rep
  135. updateCases(opt, otmTemp, casesUpdate);
  136. }
  137.  
  138. // Helper method to update Engineering Cases for specific roles
  139. private void updateCases(Opportunity opt, OpportunityTeamMember otmTemp, Map<Id, Case> casesUpdate) {
  140. for (Case currCase : opt.Engineering_Requests__r) {
  141. currCase.SalesRep__c = otmTemp.UserId;
  142. casesUpdate.put(currCase.Id, currCase);
  143. }
  144. }
  145.  
  146. // Helper method to update EPM Manager
  147. private void updateEPM(Opportunity opt, OpportunityTeamMember otmTemp,
  148. Map<String, Integer> stusCnt, Map<Id, Case> casesUpdate) {
  149. if (stusCnt.get(Constants.EPM_STATUS) == 1) {
  150. opt.EPM__c = otmTemp.UserId;
  151. } else if (stusCnt.get(Constants.EPM_STATUS) > 1) {
  152. if ((opt.EPM__c == null || !opt.EPM__r.isActive) && opt.EPM__c != otmTemp.UserId) {
  153. opt.EPM__c = otmTemp.UserId;
  154. }
  155. }
  156.  
  157. // Update related Cases with new EPM
  158. updateCases(opt, otmTemp, casesUpdate);
  159. }
  160. }
  161.  
Success #stdin #stdout #stderr 0.02s 12140KB
stdin
Standard input is empty
stdout
Object: UndefinedObject error: did not understand #'ProjectISRAssociation_Batch'
MessageNotUnderstood(Exception)>>signal (ExcHandling.st:254)
UndefinedObject class(Object)>>doesNotUnderstand: #'ProjectISRAssociation_Batch' (SysExcept.st:1448)
UndefinedObject>>executeStatements (prog:1)
Object: nil error: did not understand #associationAt:ifAbsent:
MessageNotUnderstood(Exception)>>signal (ExcHandling.st:254)
UndefinedObject(Object)>>doesNotUnderstand: #associationAt:ifAbsent: (SysExcept.st:1448)
DeferredVariableBinding>>resolvePathFrom: (DeferBinding.st:115)
DeferredVariableBinding>>value (DeferBinding.st:69)
UndefinedObject>>executeStatements (prog:50)
Object: nil error: did not understand #associationAt:ifAbsent:
MessageNotUnderstood(Exception)>>signal (ExcHandling.st:254)
UndefinedObject(Object)>>doesNotUnderstand: #associationAt:ifAbsent: (SysExcept.st:1448)
DeferredVariableBinding>>resolvePathFrom: (DeferBinding.st:115)
DeferredVariableBinding>>value (DeferBinding.st:69)
UndefinedObject>>executeStatements (prog:54)
stderr
./prog:1: expected expression
./prog:50: expected expression
./prog:54: expected expression
./prog:161: Unterminated string, attempting recovery