Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. public class AccountOwnerAssignment {
  2.  
  3. public static void AssignOwners(List<Account> triggerOld,List<Account> triggerNew){
  4.  
  5. List <User> nonNamedAMs = [SELECT ID, LastName FROM User WHERE AM_Team_Account_Tier__c = 'Non-Named'AND isActive = TRUE ORDER BY lastName DESC];
  6. List <RoundRobinAssignment__mdt> accountReassignmentNumberRecord = [SELECT AccountReassignmentNumber__c FROM RoundRobinAssignment__mdt WHERE Label = 'Master'];
  7. Integer currentOwnerPosition = accountReassignmentNumberRecord[0].AccountReassignmentNumber__c.intValue();
  8.  
  9. FOR(integer i=0;i<triggerNew.size();i++){
  10. //Confirms that the Number of Clients have increased from 0 to a greater number
  11. IF(triggerNew[i].No_of_Clients__c > 0 && triggerOld[i].No_of_Clients__c == 0){
  12. currentOwnerPosition = calculatePosition(currentOwnerPosition, nonNamedAMS.Size());
  13. System.debug('currentOwnerPosition ' + currentOwnerPosition);
  14. System.debug('nonNamedAMs.size() ' + nonNamedAMs.size());
  15. System.debug('nonNamedAMs[currentOwnerPosition].ID ' + nonNamedAMs[currentOwnerPosition].ID + 'nonNamedAMs[currentOwnerPosition].LastName ' + nonNamedAMs[currentOwnerPosition].LastName);
  16.  
  17. //Update Account with new Account Owner
  18. triggerNew[i].OwnerId= nonNamedAMs[currentOwnerPosition].ID;
  19. System.debug('triggerNew[i].OwnerId ' + triggerNew[i].OwnerId);
  20. }
  21. }
  22. // Update the AccountReassignmentNumber to the last AMs postion on the RoundRobinAssignment Custom Metadata Type record
  23. accountReassignmentNumberRecord[0].AccountReassignmentNumber__c = currentOwnerPosition;
  24. //Commit update to the AccountReassignmentNumber__c How do I do this?
  25. }
  26. //IF the current positon is greater than the total AM count then the counter is reset else we increment to the next AM
  27. public static Integer calculatePosition(integer currentOwnerPosition, integer amCount){
  28. IF(amCount <= currentOwnerPosition){
  29. currentOwnerPosition = 0;
  30. } else {
  31. currentOwnerPosition = currentOwnerPosition+1;}
  32. System.debug('New currentOwnerPosition = ' + currentOwnerPosition);
  33. return currentOwnerPosition;
  34. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement