Custom Round Robin Assignment with Apex and Custom Settings: A Comprehensive Guide

Efficient lead management is crucial for any sales team aiming to maximize productivity. In this blog post, we will explore how to implement a custom Round Robin lead assignment system in Salesforce using Apex and Custom Settings. This solution ensures that leads are evenly distributed among users, taking into account user availability and any scheduled leaves.

What is Round Robin Assignment?

Round Robin assignment is a method of distributing leads or tasks evenly among a group of users. This ensures that no single user is overwhelmed while others may be underutilized. In our approach, we will use a counter stored in Custom Settings to track which user should receive the next lead assignment.

Key Components of the Solution

1. Custom Settings for Configuration

We will use a Custom Setting called Round_Robbin__c to store the following:

  • Group Name: The name of the user group involved in lead assignments.
  • Counter: An integer that keeps track of which user should receive the next lead.

2. User Availability Tracking

To ensure leads are assigned only to available users, we will create a custom multi-picklist formula field on the User object to indicate the days they are on leave. This field will help in determining user availability during the assignment process.

3. Apex Class for Lead Assignment

The core of our solution is an Apex class that will manage the lead assignment logic. Here's a breakdown of how this class operates:

Code Implementation

public class RoundRobbinAssigner { @InvocableMethod(label='Round Robbin Assigner' description='Fetches the next counterIndex Person') public static List<ReturnVariables> indexcalculator(List<InputVariables> leadRecords) { List<ReturnVariables> retVal = new List<ReturnVariables>(); Round_Robbin__c rb = Round_Robbin__c.getValues('RoundRobbinCounter'); if (rb != null) { Set<Id> userId = new Set<Id>(); for (GroupMember gm : [SELECT UserorGroupId FROM GroupMember WHERE Group.Name = :rb.Group_Name__c]) { userId.add(gm.UserorGroupId); } if (!userId.isEmpty()) { Integer counter = (Integer)rb.Counter__c; List<User> allUsersWorking = [SELECT Id FROM User WHERE Id IN :userId AND isWorking__c = true ORDER BY Id ASC]; if (allUsersWorking.isEmpty()) { for (InputVariables inputVar : leadRecords) { ReturnVariables currentReturnvalue = new ReturnVariables(); currentReturnvalue.currentId = 'NO_ACTIVE_USERS'; retVal.add(currentReturnvalue); } return retVal; } counter = Math.max(counter, 0); counter = counter >= allUsersWorking.size() ? 0 : counter; for (InputVariables inptVar : leadRecords) { ReturnVariables currentReturnvalue = new ReturnVariables(); currentReturnvalue.currentId = allUsersWorking[counter].Id; retVal.add(currentReturnvalue); counter++; if (counter >= allUsersWorking.size()) { counter = 0; } } rb.Counter__c = counter; update rb; return retVal; } } for (InputVariables inputVar : leadRecords) { ReturnVariables currentReturnvalue = new ReturnVariables(); currentReturnvalue.currentId = 'NO_USER'; retVal.add(currentReturnvalue); } return retVal; } public class InputVariables { @InvocableVariable public String inputId; } public class ReturnVariables { @InvocableVariable public String currentId; } }

4. How the Apex Class Works

  • Configuration Retrieval: The class starts by fetching the Round Robin configuration using Custom Settings.
  • User Identification: It gathers user IDs from the specified group, filtering out those who are unavailable.
  • Safety Checks: The class checks for available users and adjusts the counter to prevent overflow.
  • Round Robin Logic: It assigns the next lead to the user based on the counter, ensuring an even distribution of leads.
  • Counter Update: After assignment, the counter is updated in the Custom Settings to track the next user for future assignments.

Benefits of This Approach

  • Fair Lead Distribution: Ensures an even workload across your team, improving morale and productivity.
  • User Availability Consideration: Automatically avoids assigning leads to users who are on leave.
  • Customizable Configuration: Easily adjust group membership and user settings via Custom Settings.

Comments

Popular posts from this blog

How to Get a Salesforce Access Token from LWC Using Client Credentials Flow

Geolocation in Lightning Web Components (LWC)