function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kunal Purohit 4Kunal Purohit 4 

How to rewrite below handler?

Hello Folks,
Below Handler works on Round Robin manner in contact Object. Such that, when Contact record is created , it is assigned to another System Admin, based on Round Robin Manner. It is for 2 users only. What changes should I made so that it could work for multiple users. Please Suggest.

public class RoundRobinContactHandler {
		public static void AssignUser(List<Contact> cn)
        {
            integer countContact = 0;
            String usr;
            List<Contact> cnList = new List<Contact> ();
            
                 
            List<Contact> clist=[Select id, Name, Assigned_To__c from Contact where Assigned_To__r.isActive = TRUE];
            countContact=clist.size();
            integer RnRUserAssigned = Math.mod(countContact, 2) + 1;
            integer CSRindex=0;
            
            if(RnRUserAssigned==1)
            {
                usr=[Select id, name from User where Id = '0052w000002n0Pj' LIMIT 1].id;
            }
            else if (RnRUserAssigned==2)
            {
                usr=[Select id, name from User where Id = '0052w000009deaY' LIMIT 1].id;
            }
            
            if(cn.size()>0 && (usr!=null && usr!='')) 
            {
                for(Contact con : cn)
            	{
                  con.Assigned_To__c= usr;
                 
             	} 
            }
           
         }
}
Rakesh kumar 708Rakesh kumar 708

To rewrite the handler to work for multiple users, you will need to modify the logic that determines which user to assign the contact to. Here is one way you could rewrite the handler to support multiple users:

 

public class RoundRobinContactHandler {

    // Replace this list with a list of the IDs of the users you want to assign contacts to
    private static List<Id> userIds = new List<Id> {
        '0052w000002n0Pj',
        '0052w000009deaY',
        // Add more user IDs here as needed
    };

    public static void AssignUser(List<Contact> cn) {
        // Get a list of all active users
        List<User> activeUsers = [SELECT Id FROM User WHERE IsActive = TRUE];
        // Filter the list of active users to only include the users we want to assign contacts to
        activeUsers.retainAll(userIds);

        // If there are no users to assign contacts to, do nothing
        if (activeUsers.isEmpty()) {
            return;
        }

        // Get a count of the number of contacts that have already been assigned to active users
        integer countContacts = [SELECT COUNT() FROM Contact WHERE Assigned_To__r.IsActive = TRUE];
        // Calculate the index of the user to assign the next contact to, using a round-robin approach
        integer userIndex = Math.mod(countContacts, activeUsers.size());

        // Assign the contacts to the appropriate user
        for (Contact con : cn) {
            con.Assigned_To__c = activeUsers[userIndex].Id;
            // Increment the user index, wrapping around to the beginning if necessary
            userIndex = (userIndex + 1) % activeUsers.size();
        }
    }
}
 

 

Hope this helps

Some useful Greetings and Quotes for Everyone 🤗

 

💛Good Morning Image in Hindi 

💛Happy new year 2023 images and Greetings

🥰 Romantic Good Night Images

😍 Good Night Images

🌆 Good Evening Images

🌼 Good Afternoon Images

🌷 Good Morning Images

🎄 Free Jesus Christmas Images

🔴Not to-do list for Students

🔴DO NOT LIST—On Finance and Investment

🔴DO NOT LIST—From the Book of Bible

🔴NOT TO DO LIST—Journals!

🔴NOT TO DO LIST—Christmas Special

🔴Bible Quotes in Malayalam

🔴Bible quotes in Telugu

💚4 Advantages of Sending Good-Night Images and Images

💚2 Secret Greeting images to impress friends, family, and lover

💚Why we must send good morning images to friends and loved ones!