+ Start a Discussion
Connor ReillyConnor Reilly 

Lead Round Robin - distributing to two groups based on various criteria

Hi, 

I have a requirement where I need to round robin leads to two groups of users (SDRs and Account Execs). If lead has xyz fields filled out, it should round robin to the account execs. However, if the fields are not filled out, it should round robin to SDRs. Has anyone seen a good way to do this?
vijay kumar kvijay kumar k
Hi Connor Reilly

You can achieve this using trigger
Trigger roundrobintrigger on Lead (Before Insert){
    //I have Created number field storing round roubin number based on xyz field
    // We can use here either custom metadata or custom setting for declaring pre values of persons.
    //like we have 20 people in SDRs pre values are persion1=>1,Person2=>2,Person3=>3....etc
    //using this values we allocating users and WHY Custom metadata or custom settings is users may come or leaving any time.,
    //so if you do not want any on queue you can remove from either custom metadata or cutom settings.
    // Now I'm using Custom Settings (Type = List) and I created one custom field called round roin id(pre value) //and while creating records here insted of using user name use userid.
    //Now Querying customsettings records and puuting in map based rounrobin id
    Map<Integer,String>SDRsusersMap =New Map<Integer,String>();
    For(SDRsusersList__c sdr:[Select id,Name,Round_roubin_Id__c from SDRsusersList__c]){
        SDRsusersMap.put(sdr.Round_roubin_Id__c,sdr.name);//Name is nothing but a user id.
    }
    // similarly for AccountExecs group
    Map<Integer,String>AccountExecsusersMap =New Map<Integer,String>();
    For(AccountExecsusersList__c acc:[Select id,Name,Round_roubin_Id__c from AccountExecsusersList__c]){
        AccountExecsusersMap.put(acc.Round_roubin_Id__c,acc.name);//Name is nothing but a user id.
    }
    Boolean isxyzexist=false;
    Boolean notisxyzexist=false;
    for(Lead l:Trigger.New){
        if(l.isxyzexist__c!=null){
            isxyzexist=true;
        }
        else {
            notisxyzexist=true;
        }
    }
    
    List<Lead> SDRsList=new List<Lead>();
    List<Lead> AccountExecsList=new List<Lead>();
    Integer SDRsnextNumber=0;
    Integer AccountExecsnextNumber=0;
    if(isxyzexist){
        SDRsList = [Select id,Round_roubin_Id__c from Lead where 
                    Round_roubin_Id__c != null and isxyzexist__c!=null order by Round_roubin_Id__c desc limit 1];
        if(SDRsList!=null && !SDRsList.isEmpty()){
            if(SDRsList[0].Round_roubin_Id__c !=null)
                SDRsnextNumber=Integer.valueOf(SDRsList[0].Round_roubin_Id__c );
        }
    }
    if(notisxyzexist){
        AccountExecsList = [Select id,Round_roubin_Id__c from Case where 
                            Round_roubin_Id__c != null and isxyzexist__c=null order by Round_roubin_Id__c desc limit 1];
        
        if(AccountExecsList!=null && !AccountExecsList.isEmpty()){
            if(AccountExecsList[0].Round_roubin_Id__c !=null)
                AccountExecsnextNumber=Integer.valueOf(AccountExecsList[0].Round_roubin_Id__c );
        }
    }
    for(Lead it : Trigger.New){
        if(it.isxyzexist__c !=Null){
            if(it.Round_roubin_Id__c == null){
                SDRsnextNumber=SDRsnextNumber+1;
                it.Round_roubin_Id__c = SDRsnextNumber;
                it.OwnerId=SDRsusersMap.get(SDRsnextNumber);
                if(SDRsnextNumber==20)//why Im assuming for 20 members so if reached again restarting..you can put //what number u want.
                    SDRsnextNumber=0;
            }  
        } 
        else if(it.isxyzexist__c ==Null){
            if(it.Round_roubin_Id__c == null){
                AccountExecsnextNumber = AccountExecsnextNumber+1; 
                it.Round_roubin_Id__c = AccountExecsnextNumber;
                it.OwnerId=AccountExecsusersMap.get(AccountExecsnextNumber);
                if(AccountExecsnextNumber==20)//This users count also no need to give hard code you can declare //size of that map like AccountExecsnextNumber==AccountExecsusersMap.size() this will give count of users in that //group
                    AccountExecsnextNumber=0;
            } 
        } 
    } 
}



I have given my solution to my understanding. I hope this is useful to you. If I understand wrongly explain in detail Ill figure out the good solution.

Regards
Vijay
Connor ReillyConnor Reilly

Hi Vijay, this is great! Thank you. 

So if I have multiple fields that would determine if it goes to an Account Exec, would I add them here:

Boolean isxyzexist=false;
    Boolean notisxyzexist=false;
    for(Lead l:Trigger.New){
        if(l.isxyzexist__c!=null){
            isxyzexist=true;
        }
        else {
            notisxyzexist=true;
        }
    }
For example: I need to say, "if process requested is != null AND != 'unsure', then assign to Account Exec round robin"
Process requested is a picklist field.

Another note: the trigger should only fire if the created by user is "Marketing" 

Thanks,
Connor
vijay kumar kvijay kumar k

 

Hi Conor

You can multiple if conditions like
Boolean isxyzexist=false;
    Boolean notisxyzexist=false;
    for(Lead l:Trigger.New){
        if(l.isxyzexist__c!=null && l.isxyzexist__c!='unsure'){
            isxyzexist=true;
        }
        else if(l.isxyzexist__c==null){
            notisxyzexist=true;
        }
    }

//or
/* else if(l.isxyzexist__c=='unsure'){
            notisxyzexist=true;
        }*///it will go to account exces

Entire logic will run based on the if and else if conditions. You can change the entire logic as per your convenience.
.
Another note: the trigger should only fire if the created by user is "Marketing" -use if conditions Like
​​​​​​​if(Lead.Owner.Profile.Name=='Marketing'){
//logic
}
 
Connor ReillyConnor Reilly
Thanks Vijay. In regards to the roundrobinid__c. Does this field need to be created on the Lead object as well as in the custom setting?
SDRsList = [Select id,Round_robin_id__c from Lead where 
                    Round_robin_id__c != null and isxyzexist__c!=null order by Round_robin_id__c desc limit 1];
        if(SDRsList!=null && !SDRsList.isEmpty()){
            if(SDRsList[0].Round_robin_Id__c !=null)
                SDRsnextNumber=Integer.valueOf(SDRsList[0].Round_robin_id__c );
        }
I'm getting an error on line 34 indicating that the field isn't on the Lead object.

Thanks!