I need to update lead owner in Round-Robin fashion. for this i have custom settings records (List type as a refrence along with course and batch fields).If leads are inserted i need to check the course and batch from custom setting(We will bee having two records).

So when first lead is inserted owner should be User1(user id will be getting from custom setting records)

when Second lead is inserted owner should be User2 and this process should be repeated .I have no idea how to assign in round robin fashion. for the time being i have assigned by passing index value.

Can anyone help me how to solve the issue.

Here LeadOwnerAssignee__c is custom setting object

Trigger :

trigger LeadTrigger on Lead (before insert, before update,after insert, after update) {
     String triggerEvent = '';
     LeadTriggerHelper obj = NEW LeadTriggerHelper (); 
     if (Trigger.isbefore && Trigger.isInsert)
     triggerEvent = 'Before-Insert';

Helper Class :

public class LeadTriggerHelper {
    public Map<String,List<LeadOwnerAssignee__c>> BatchCourseWithOwners = new Map<String,List<LeadOwnerAssignee__c>>(); 
    public Map<String,List<Decimal>> BatchCourseRecordCount = new Map<String,List<Decimal>>();
    public Set<String> courses = new set<String>();
    public set<String> Buckets = new set<String>();
    public string bucketCourse;
    public  void RoundRobinOwnerAssignee(string event,List<Lead> newLeads) {
                if(event == 'Before-Insert') {
                    for(Lead eachLead : newLeads) {
                        if(eachLead.Bucket__c != null && eachLead.Course_Interest__c != null){
                    List<LeadOwnerAssignee__c> leadOwners = [SELECT Batch_Number__c,Course__c,Owner__c,Record_Count__c
                                                             FROM LeadOwnerAssignee__c 
                                                             WHERE Batch_Number__c in :Buckets 
                                                             AND Course__c in : courses];
                    for(LeadOwnerAssignee__c eachowner : leadOwners) {
                        bucketCourse =  eachowner.Course__c + ' ' + eachowner.Batch_Number__c ;
                        if(BatchCourseWithOwners.containsKey(bucketCourse)) {
                        else {
                            BatchCourseWithOwners.put(bucketCourse, new List<LeadOwnerAssignee__c>{eachOwner});
                            BatchCourseRecordCount.put(bucketCourse , new List<Decimal> {eachowner.Record_Count__c});
                        bucketCourse = '';
                    for(Lead eachLead : newLeads) {
                        string selectedCourseBucket = eachLead.Course_Interest__c + ' ' + eachLead.Bucket__c ;
                        if(BatchCourseWithOwners.containsKey(selectedCourseBucket)) {
                            eachLead.OwnerId = BatchCourseWithOwners.get(selectedCourseBucket)[0].Owner__c;

1 Answer 1


You can take a reference from here https://help.salesforce.com/s/articleView?id=sf.essentials_round_robin_lead_assignment.htm&type=5

It should give you an idea to progress your solution!

  • Welcome to Salesforce Stack Exchange (SFSE). Link-only answers are discouraged, and may be deleted. (See: Why and how are some answers deleted?) Please edit you answer to add more information. (From How do I write a good answer?: "...please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link.... ")
    – Moonpie
    Oct 1, 2021 at 18:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .