[SalesForce] How to make a round robin that evenly assigns leads within multiple, separate teams

I'm already very familiar with the standard method for implementing a round robin, but the normal approach one big drawback in my scenario.

I have two separate teams (East and West), and all of our incoming leads get assigned to one or the other team based on territory. The problem is that the Round Robin ID field modulates over all the inbound leads.

To illustrate, let's say each team has two reps, so the Round Robin ID modulates between values of 1 and 2. If we get a perfect alternation between east and west leads coming in, then all the East leads will have Round Robin ID 1, and all the West leads will have Round Robin ID 2. So one rep on each team will get all the leads.

Of course it doesn't happen this perfectly, but there are days when the balance of leads between team members gets pretty messed up and we get a lot of complaints, despite the fact that in the long run it will generally balance out.

In any case, is there a way to have separate round robin IDs that only apply to specific sets of leads?


Thanks @sfdcfox for the suggestion. I have created the custom settings and fields as you suggested, and I have written an apex trigger on the Lead object – when a Lead is inserted, it checks the lead's sales_territory__c field for East or West, then finds the current value of that instance of the custom setting split_round_robins__c, copies the value over to the lead – either to `round_robin_counter_east' or 'round_robin_counter_west', and then increments the custom setting instance.

However, I'm having trouble getting the trigger to pass testing. It keeps throwing this error:

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, splitRoundRobin: execution of BeforeInsert
caused by: System.NullPointerException: Attempt to de-reference a null object
Trigger.splitRoundRobin: line 11, column 1: []`

Here is my trigger code:

trigger splitRoundRobin on Lead (before insert) {
    for (Lead newLead : Trigger.new) {  

        if ( newLead.Sales_territory__c == 'East' ){

            split_round_robins__c myPull = split_round_robins__c.getInstance('East');
            newLead.Round_robin_counter_east__c = myPull.Territory__c;

            myPull.Territory__c++;

        }  else if (newLead.Country__c != 'Brazil') {

            split_round_robins__c myPull = split_round_robins__c.getInstance('West');
            newLead.Round_robin_counter_west__c = myPull.Territory__c;

            myPull.Territory__c++; 
        }
    }
}

and my test class:

@istest

public class SplitRRTriggerTest {
    static testMethod void insertNewLead() {

    Lead ll = new Lead();

    ll.FirstName = 'Mr.';
    ll.LastName = 'Jones';
    ll.Company = 'Corp';
    ll.Status = 'Open';
    ll.Sales_territory__c = 'West';
    ll.Country__c = 'United States';

    insert ll;


    }

}

(We have one member of the West team who automatically gets all Brazil leads, otherwise if a lead is not an "East" lead it will just go to the West team.)

But why can't I get the trigger to pass the test execution?

Best Answer

You could keep track of each group by custom settings. This implies you'll need some apex code to determine the next value to use.

Start with a custom list setting. It should have one custom field, a number. The name value will be some specific value for each queue, say East or West.

Then, you have a trigger, before insert. Have the trigger determine if each lead will go East or West. Copy the value from the appropriate custom setting to a custom field that is hidden from users, and increment the value afterwards.

The assignment rules can be built the way they are today, using the custom field value and the East or West designation, as appropriate.

Other alternatives are available, as well, such as an asynchronous call after creation to reassign the leads afterwards. The trick is the custom setting, to track which user is next in line for the round robin assignment.

Related Topic