Assign leads by creation time

Automatically assign new leads in Zoho CRM based on creation time to ensure timely follow-ups across all shifts.

For businesses with distributed sales teams or round-the-clock operations, assigning leads based on their creation times is important. By routing leads to sales reps who are available during specific hours, you can ensure timely follow-ups and avoid missed opportunities.

Although manual monitoring and assignment are possible, this approach becomes inefficient as lead volume increases. While round-robin assignment helps distribute leads equitably among sales reps, it doesn't account for their availability throughout the day. To address this limitation, you can use a custom function alongside a workflow rule.

With functions, you can implement an intelligent time-based routing system that assigns leads to different sales reps based on when they're created in your CRM. This ensures that leads are immediately assigned to available team members, reduces response times and improves the overall customer experience. To learn more, please read the sections below.

Permissions and availability

  • Users with the Manage Extensibility permission can create connections 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

  • Identify and document your sales reps' User IDs for each shift.
  • Create a Zoho CRM connection with the required scope as detailed in the "Create a connection" section below. Learn more
  • Create a workflow rule for the Leads module that triggers every time a new lead is created. Learn more
  • Write a custom Deluge function that assigns new leads based on the hour they were created in your CRM. Learn more
  • Test the workflow rule in a sandbox before deploying it in your production environment. Learn more

Create a connection

The first step is to create a connection in your Zoho CRM account.

  1. Navigate to Setup > Developer Hub > Connections.
  2. Click Create Connection.
  3. Select Zoho CRM under Default Services.
  4. Specify a Connection Name.
    You must 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 to the requested scopes.

Create a workflow rule

Next, create a workflow rule for the Leads module that triggers every time a new lead is created.

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

The code

Code Copied
resp = invokeurl
[
	url :"https://www.zohoapis.com/crm/v8/Leads/" + leadId
	type :GET
	connection:"connectionname"
];
info resp;
leadDetails = resp.get("data").get(0);
createdtime = ifnull(leadDetails.get("Created_Time"),"");
createdhour = createdtime.toTime("yyyy-MM-dd'T'HH:mm:ss").getHour();
info createdhour;
if(createdhour >= 9 && createdhour < 17)
{
	ownerId = "USERID1";
}
else if(createdhour >= 17 && createdhour < 1)
{
	ownerId = "USERID2";
}
else
{
	ownerId = "USERID3";
}
mp = Map();
mp.put("Owner",ownerId);
dlist = List();
dlist.add(mp);
dmp = Map();
dmp.put("data",dlist);
update = invokeurl
[
	url :"https://www.zohoapis.com/crm/v8/Leads/" + leadId
	type :PUT
	parameters:dmp + ""
	connection:"connectionname"
];
info dmp;
info update;

Notes

  • Make sure to use the accurate API names for their corresponding fields (e.g., "Owner") in the code snippet. Learn more
  • Replace "connectionname" with the name of the connection you specified when creating it (see the "Create a connection" section above).
  • Replace "USERID1", "USERID2", and "USERID3" with the actual user IDs from your CRM. To get the user ID, navigate to Setup → Users, select a user, and copy the numeric ID from the URL. That embedded number is the User ID.
  • Customize the time ranges in the code to assign leads to specific sales reps based on when they were created. For example, the code snippet assigns leads created between 09:00 and 17:00 to one sales rep, those created between 17:00 and 01:00 to another, and all other leads to a third rep. Use only 24-hour time to set up the custom time windows.
  • Change the URL endpoint to match your account's data center. For example, https://www.zohoapis.in/... for India.
  • The code above is an example of assigning lead records to three sales reps in your organization who work during different shifts. You can use this code for any other module by changing the module name and parameters.

Tip

  • Configure and test the solution 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 owner was updated based on the creation hour.
  4. Test edge cases, such as by creating leads at key transition times (9:00 AM, 5:00 PM, and 1:00 AM) and creating multiple leads in quick succession.

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
  • ComplexityMedium
  • Implementation Time30 minutes

Features used in the solution

ConnectionWorkflow 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!