Salesforce Trigger for Lead Round Robin Assignment

Naga Alapati
2 min readMar 11, 2020

--

We can assign leads to the users in a particular queue through round robin algorithm in many ways. I tried to implement this functionality programmatically by using custom settings and apex triggers in this post.

Round robin assignment is nothing but distribution of leads among the group of people in a rotational manner. Suppose if there are 3 reps in the queue and we want to distribute the leads in round robin manner then

  • Lead1 goes to Rep1
  • Lead2 goes to Rep2
  • Lead3 goes to Rep3
  • Lead4 goes to Rep1
  • and so on

For this one, first step is to create a queue named “Lead Queue” and add some users to that. Whenever a lead is assigned to that queue, we need to reassign the lead to one of the users in the queue in a Round robin manner using Trigger.

Queue creation to hold all the leads

We need to know for what user in the queue that we assigned the last lead. So for that we are going to use custom settings to hold the index of the last assigned user in the queue. Next step is to create a custom settings named “Lead Round Robin Assignment” and the custom field named “User Index” as shown below.

Custom settings to hold the index of last lead assigned user

Also create a new record for Lead round Robin assignment custom settings and set the value of user index to -1.

Default Lead Round Robin Assignment record

Final step is to write the trigger on lead to reassign the leads in a round robin manner

Lead Trigger

In order to test these changes insert some test leads with this anonymous code:

Anonymous code to insert test leads

Check for the above inserted leads and you can see that the leads are assigned to the users in a round robin manner.

We can also update the logic based on the working hours of the reps too and will try to explain it in another post. That’s it for now.

--

--