Automatic assignment of lead owner to the contact with least number of leads under him.

I’m working on a business process that randomly assigns contacts as owners of new leads, but only after checking how many leads are already assigned to each contact. The goal is to ensure that each new lead is assigned to the contact who currently owns the fewest leads.

I need guidance or a solution for implementing this logic so that the system correctly identifies the contact with the lowest lead count and assigns the new lead to them.

Purpose is to distribute leads fairly and ensure every lead gets timely attention.

Like 0

Like

2 comments

What I would likely do is:

  1. Create a database view that provides a list of users that are potential lead owners with a count of open leads for each (see sample below)
  2. Expose the view as an object (see here)
  3. In a process when a new lead is created, add a Read Data element to read from the object above and sort by the open lead count in ascending order. The user read will be the one with the fewest open leads to assign the lead to.

The view could be something like this (this is for postgres and is not tested). You might need to adjust which users are included if you don't want to consider all users (maybe with a join to SysUserInRole to check if they belong to a role?)

create or replace view "UsrVwLeadOwnerCount"
as
 
select 
    con."Id",
    con."CreatedById",
    con."CreatedOn",
    con."ModifiedById",
    con."ModifiedOn",
    con."ProcessListeners",
    con."Id" as "UsrContactId",
    coalesce(leadstat."NumLeads", 0) as "UsrOpenLeads"
from 
    "Contact" con
 
    inner join "SysAdminUnit" adm
    on con."Id" = adm."ContactId" and adm."SysAdminUnitTypeValue" = 4
 
    left join (
        select 
            l."OwnerId", 
            count(l."Id") as "NumLeads"
        from 
            "Lead" l
            inner join "QualifyStatus" qs on l."QualifyStatusId" = qs."Id"
        where 
            qs."IsFinal" = false
        group by 
            l."OwnerId"
    ) as leadstat on con."Id" = leadstat."OwnerId"
 
where
    adm."Name" not in ('Supervisor','Mandrill','SysPortalConnection')
    and 
    adm."Active" = true

(I'd possibly add another subquery for most recently assigned lead date, so you could add a secondary sort on that in case multiple users have the same open lead count). 

Ryan

Hello,

You can implement this logic in a business process, but it cannot be done using standard elements only - a Script Task is required to correctly identify the contact with the lowest lead count.

Here is the recommended approach:

1) Use a Read Data element to retrieve all eligible contacts. Make sure it returns a collection of records.

2) Use a subprocess that runs for each item in this collection. Inside the subprocess, add another Read Data element to count how many leads are currently assigned to that specific contact (using the Owner field or your custom ownership field). Pass the result back to the parent process through process parameters.

3) In the parent process, use a Script Task to analyze the collected results and determine which contact has the lowest lead count.

4) Finally, use a Modify Data element to assign the selected contact to the new lead.

This approach ensures that the system always selects the contact with the smallest workload and distributes leads fairly.

Show all comments