Assign leads in round-robin order

Automate equitable lead assignment using round-robin logic to balance workloads, improve team efficiency, and maximize sales opportunities.

In sales organizations, equitable lead distribution is critical for maintaining team morale and maximizing conversion opportunities. Manual lead assignment often results in uneven workloads, with some salespeople overwhelmed while others remain underutilized. This imbalance can lead to delayed follow-ups, missed opportunities, and ultimately lost revenue.

This solution implements an automated round-robin lead assignment system using CRM variables to track the current assignee in the rotation sequence. By storing the current owner ID in a CRM variable, the system maintains state across executions and ensures consistent, fair distribution of incoming leads. This approach eliminates manual intervention, reduces assignment errors, and provides a scalable framework that adapts as your sales team grows or changes. Read on to learn more.

Permissions and availability

  • Users with the Manage Extensibility permission can create connections, configure variables, and write custom functions.
  • Users with the Manage Automation permission can create and update workflow rules.
  • Users with the Manage Sandbox permission can manage the sandbox.

Requirements

  • Create a Zoho CRM connection with the required scopes as detailed in the "Create Connection" section below. Learn more
  • Create a single-line organization variable by specifying a sales rep's user ID in your account. Learn more
  • Create a workflow rule for the Leads module that assigns leads to the correct sales reps when they are created. Learn more
  • Write a custom Deluge function using the code snippet provided below. Learn more
  • Test the solution in a sandbox before deploying it to your production environment. Learn more

Create a connection

First, create a new connection in your Zoho CRM account.

  1. Go to Setup > Developer Hub > Connections.
  2. Click Create Connection.
  3. Select Zoho CRM under Default Services.
  4. Specify a Connection Name.
    You will need to enter this name in the code snippet below.
  5. Select the following scope to request access:
    • ZohoCRM.modules.ALL
  6. Click Create and Connect.
  7. Click Connect, then click Accept to confirm access authorization for the scope requested by the client.

Create a variable

Next, create a new single-line organization variable and set it to the required value.

  1. Navigate to Setup > Developer Hub > Variables > +Create New Variable.
  2. Provide a name and API name for the variable. For example: "roundrobinLead".
  3. Set the variable's type to Single Line.
  4. Enter the value; specifically, the user ID of the first sales rep from your code snippet (i.e., User_ID_1 in firstOwnerList).
    This value defines which sales rep gets the first lead in the round-robin.
  5. Choose where your variable should be grouped.
    You can also create a new group, if necessary.
  6. Click Save.

Create a workflow rule

Next, create a workflow rule linked to a custom function.

  1. Navigate to Setup > Workflow Rules > +Create Rule.
  2. Select the module as Leads from the dropdown list.
  3. Provide a name for the rule. For example: "Assign leads to sales reps".
  4. Add a description, if required, then click Next.
  5. In the Execute this workflow rule based on section, choose Record Action, select Create, and then click Next.
  6. In the Which leads would you like to apply the rule to? section, select All Leads, then click Done.
  7. Under Instant Actions, select Function and then Write your own.
  8. Provide a Name for the function, as well as a description, if necessary.
  9. In the Deluge Script Editor, do the following:
    1. Copy and paste the code provided below.
    2. Click Edit Arguments.
    3. Choose Leads - Lead Id, name it leadId, then click Save.
    4. Click Save & Execute.
  10. Click Save on the workflow rule page.

The code

Code Copied
olduserid = zoho.crm.getOrgVariable("apiname");
info "olduserid : " + olduserid;
firstOwnerlist = {"User_ID_1","User_ID_2","User_ID_3"};
UpOwnerlist = {"User_ID_2","User_ID_3","User_ID_1"};
oldownerindex = firstOwnerlist.indexOf(olduserid);
newownerid = UpOwnerlist.get(oldownerindex);
mp = map();
mp.put("Owner",newownerid);
dlist = List();
dlist.add(mp);
dmp = map();
dmp.put("data",dlist);
update = invokeurl
[
url :"https://www.zohoapis.com/crm/v7/Leads/" + leadId
type : PUT
parameters: dmp + ""
connection:"connectionname"
];
info dmp;
info update;
datalist = List();
mp1 = map();
mp1.put("id", "variableID");
mp1.put("value",newownerid.toLong());
mp1.put("name","apiname");
datalist.add(mp);
newMap = Map();
newMap.put("variables",datalist);
response2 = invokeurl
[
url :"https://www.zohoapis.com/crm/v7/settings/variables"
type :PUT
parameters:newMap.toString()
connection:"connectionname"
];
info "newMap : " + newMap;
info "response2 : " + response2;

Notes

  • Replace "connectionname" with the name of the connection you specified when creating it (see the "Create a connection" section above).
  • Replace "apiname" with the API name of the variable you specified when creating it (i.e., roundrobinLead).
  • Replace "variableID" with the ID of the variable you created. To get the variable ID, navigate to Setup → Variables, edit the variable, and copy the numeric ID from the URL. That embedded number is the Variable ID.
  • Assigning to a user requires the user ID and not the user name. To get the user ID, navigate to Setup → Users, click a user, and copy the numeric ID from the URL. That embedded number is the User ID.
  • The code above is an example of assigning lead records to three sales reps in your organization in a round-robin fashion. You can use this code for any other module by changing the module name and parameters.

Tip

  • Configure and test the function in a sandbox to ensure that further development doesn't disrupt your production environment.

Test the solution

  1. Navigate to the Leads module.
  2. Create a new lead record and click Save.
    The workflow rule will be triggered.
  3. Check whether the lead gets assigned to the first sales rep/user in your rotation.
  4. Create two or more leads in sequence.
  5. Verify that each lead gets assigned to the next sales rep in the rotation. Also, confirm that the rotation loops back to the first sales rep after assigning to the last one.

Did you find this useful? Try it out and let us know how it works. Share this with your team if they'd benefit from it! If you have questions, please don't hesitate to contact us.

More info

  • ModuleLeads
  • Trigger PointWorkflow Rule
  • EditionEnterprise and above
  • ComplexityHigh
  • Implementation Time60 minutes

Features used in the solution

ConnectionVariableWorkflow RuleDeluge ScriptSandbox
Story Image

Looking for a custom solution?

Contact us, we will help enhance your productivity at lightning speed.

SUBMIT REQUEST

Developers: Share your solution with our community!