Round Robin Algorithm for Contact in Salesforce

 In this blog we will create a custom Round Robin Assignment for Contacts in Salesforce. Create a custom field in Contact object and use below trigger to generate the Round Robin !

Trigger 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
trigger contacttrigger on Contact (before insert) {
    if(Trigger.isBefore){
        Double roundRobinValue = 1;
        List<Contact> conList = [SELECT Id, Round_Robin_ID__c, CreatedDate FROM Contact where Round_Robin_ID__c  != null
                                 order by CreatedDate desc limit 1];
        if(conList != null && conList.size()>0){
            roundRobinValue = conList[0].Round_Robin_ID__c;
        }
       
        
        for(Contact c : Trigger.New){
            system.debug('###Round Robin Value : '+roundRobinValue);
            if(roundRobinValue == 3){
                roundRobinValue = 1;
                c.OwnerId = '0057F000002FrWT';
            }
            else if(roundRobinValue == 2){
                roundRobinValue++; 
                c.OwnerId = '0057F000005vkd1';
            }
            else{
                roundRobinValue++; 
                c.OwnerId = '0057F000002Fyug';
            }
            c.Round_Robin_ID__c = roundRobinValue;
        }
    }
}


Checkout complete tutorial below

 If you have any question please leave a comment below.

If you would like to add something to this post please leave a comment below.
Share this blog with your friends if you find it helpful somehow !

Thanks
Keep Coding 

Post a Comment

4 Comments

  1. how to task assign to user in different queue using round robin.

    ReplyDelete
    Replies
    1. Have you tried putting queue Id in ownerId ?

      Delete
  2. Hardcoding ids isn't a good practice naa

    ReplyDelete
    Replies
    1. Yes I agree, but that was only for testing purpose. You may make this owner id process dynamically by retrieving them from a custom object :)

      Delete