0

How to implement the concept of load balancer?

I have set up the Lead Assignment rules based on the concept of Round Robin.

Incoming Leads are rotated between each Rep. Works great.

However, I now want to balance the load. i.e. If Rep A has 4 open leads, Rep B has 6 open leads and Rep c has 20 open leads, then the next incoming lead would be assigned to the Rep with least Open leads. In this case, Rep A and Rep B will get the new leads till all three Reps have equal number of open leads. Also when a new rep joins, if each of the three existing reps have 20 open leads each, the new rep will be assigned 20 leads.

0

This question has been answered here: Assign leads from few queues as per round robin rule

Or if you do not have Omni-channel licensed and do not want to install an AppExchange product, you would need to use an Apex trigger on Lead (or possibly Flow if the round-robin team is small) to achieve this functionality.

A simplified example to show the concept:

(note: this code has not been production-tested or bulk tested, it likely has bugs, and does not handle randomized assignment when there are multiple Users with an equal number of assignments, it is purely to demonstrate the general concept of one way of assigning records to Users based on number owned):

// Query the userIds of the Users who you want Leads to be assigned to
// Note: I do not know which Users you want to assign Leads to, so I
// assumed it is a Role named 'Lead_Owner' 

Set<Id> leadOwners = [SELECT Id FROM User WHERE UserRole.DeveloperName = 'Lead_Owner'];

// Use aggregate query to get Users who currently own Leads, and the count
// of Leads each one owns, sorted so the users with the fewest Leads come first

List<AggregateResult> userIdWithLeadCountList = [SELECT OwnerId, COUNT(Id) leadCount FROM Lead WHERE OwnerId IN :leadOwners GROUP BY OwnerId ORDER BY COUNT(Id)];

// Convert above results into a Map

Map<String, Integer> userIdWithLeadCountMap = new Map<String, Integer>();
for (AggregateResult userIdWithLeadCount : userIdWithLeadCountList) {
    userIdWithLeadCountMap.put(userIdWithLeadCount.OwnerId, userIdWithLeadCount.leadCount);
}

// Loop through new Leads that need to be assigned
for (Lead newLead : Trigger.new) {
    String ownerId = [your default lead owner id goes here, perhaps stored in custom metadata];

    // Note: You would need code here and above to handle situations where
    // a user owns zero Leads, but you still want that user to be
    // assigned new leads. I have skipped that for simplicity

    // Loop through UserIds
    for (String userId : userIdWithLeadCountMap.keySet()) {

        // Check whether the currently specified owner has a fewer or equal count
        // of Leads assigned than the User Id we are looping through
        // Since we sorted by count of assigned Leads in the earlier query,
        // we can move on to the next Lead if yes
        if (userIdWithLeadCountMap.get(ownerId) <= userIdWithLeadCountMap.get(userId)) {
            break;
        } else {
            ownerId = userId;
        }
    }
    newLead.OwnerId = ownerId;
} // I am assuming this is a before insert trigger, so no DML update needed
2
  • Thanks @jamesquinn. do you have a reference to a lead load balancer algorithm. ? Feb 12 at 15:53
  • 1
    @GroundToCloud I added a primitive example to my answer. I do not know of a robust algorithm available for free. There are AppExchange products for this too Feb 12 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.