Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class AccountOwnerAssignment {
- public static void AssignOwners(List<Account> triggerOld,List<Account> triggerNew){
- List <User> nonNamedAMs = [SELECT ID, LastName FROM User WHERE AM_Team_Account_Tier__c = 'Non-Named'AND isActive = TRUE ORDER BY lastName DESC];
- List <RoundRobinAssignment__mdt> accountReassignmentNumberRecord = [SELECT AccountReassignmentNumber__c FROM RoundRobinAssignment__mdt WHERE Label = 'Master'];
- Integer currentOwnerPosition = accountReassignmentNumberRecord[0].AccountReassignmentNumber__c.intValue();
- FOR(integer i=0;i<triggerNew.size();i++){
- //Confirms that the Number of Clients have increased from 0 to a greater number
- IF(triggerNew[i].No_of_Clients__c > 0 && triggerOld[i].No_of_Clients__c == 0){
- currentOwnerPosition = calculatePosition(currentOwnerPosition, nonNamedAMS.Size());
- System.debug('currentOwnerPosition ' + currentOwnerPosition);
- System.debug('nonNamedAMs.size() ' + nonNamedAMs.size());
- System.debug('nonNamedAMs[currentOwnerPosition].ID ' + nonNamedAMs[currentOwnerPosition].ID + 'nonNamedAMs[currentOwnerPosition].LastName ' + nonNamedAMs[currentOwnerPosition].LastName);
- //Update Account with new Account Owner
- triggerNew[i].OwnerId= nonNamedAMs[currentOwnerPosition].ID;
- System.debug('triggerNew[i].OwnerId ' + triggerNew[i].OwnerId);
- }
- }
- // Update the AccountReassignmentNumber to the last AMs postion on the RoundRobinAssignment Custom Metadata Type record
- accountReassignmentNumberRecord[0].AccountReassignmentNumber__c = currentOwnerPosition;
- //Commit update to the AccountReassignmentNumber__c How do I do this?
- }
- //IF the current positon is greater than the total AM count then the counter is reset else we increment to the next AM
- public static Integer calculatePosition(integer currentOwnerPosition, integer amCount){
- IF(amCount <= currentOwnerPosition){
- currentOwnerPosition = 0;
- } else {
- currentOwnerPosition = currentOwnerPosition+1;}
- System.debug('New currentOwnerPosition = ' + currentOwnerPosition);
- return currentOwnerPosition;
- }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement