Custom Code for weighted lead rotation

Hi,
Hoping someone could help. Recently we have built workflows with a custom code for a weighted lead rotation.

We used the communtiy github code as example. Then we modified it for our use case.
Our problem is now that the code use a random percentage for the next contact to give it to a Sales Agent. We need insteat something like the Workflow uses “Rotate record to owner” to give everbody the same amount of leads.

To give you a context:
We run the code with around 300 Leads and get the following result with 12,5% per Sales Agent:

So some Sales Agents get’s up to ~40% more Leads then others. That’s the issue we ran into if we do this daily.
Is there a sulution to fix this problem for our custom code? The basic WF action “Rotate record to owner” is not sadly not an option for us.

Thanks in advance :slightly_smiling_face:

Hi @LKessler,

When it comes to weighted distribution, I highly recommend looking into Distributely. This is a certified HubSpot app that allows you to set up manual and automatic weighted distibution. I recommend checking it out!

Hey @LKessler the code in the community github just randomly selects an owner and assigns, it doesn’t use any logic, if you read the comment at the top it even outlines this “it is possible that the actual selections will not exactly match the given distribution”

In order to add logic to this, you’ll need a way to pull a number of how many contacts an owner currently has assigned. This could actually be quite intensive data to gather.

You’d need to use something like the CRM search api to search for contacts assigned to a specific user, or pull all contacts using the contacts api to get the data, then count it using custom code. This would need to feedback to your custom code workflow to inform which owners have more assignments to others and assign out appropriately.

If you have a smaller amount of sales people that aren’t changing a lot, you could potentially use active lists for each assignee to count how many contacts they have assigned, then pull these list numbers into your custom code. However this would require maintainence and code updates each time you have a new or remove an owner.

Realistically this would all take a good bit of development skill so you’ll need experience using apis and writing custom code, or have a developer!

If this isn’t an option it might be worth looking into some third party integrations like Olivia mentioned!