How to Auto assign a new Lead to Queue or User in Salesforce?

There are various  Lead assignment options available in Salesforce. In this Article we are going to use (1) Round Robin method, where we are going to use a bit of coding and (2) Assigning Leads to Queue or User without coding.

1) Round Robin method:

To auto-assign new Leads to a Queue or User in Salesforce using a round-robin method without using Process Builder, you can achieve this through Apex code. Below is an outline of the steps involved:

  • Create a Custom Field: Create a custom field on the Lead object to store the last assigned user or queue.
  • Create a Queue: You can create a Queue for assignment or assign to users directly.
  • Write an Apex Trigger: Write an Apex trigger on the Lead object that fires whenever a new Lead is created.
  • Query Users or Queues: Retrieve the list of users or queues that you want to assign Leads to in a round-robin fashion.
  • Determine Next Assignee: Use logic to determine the next user or queue to assign the Lead to based on the last assigned user/queue and the list of users/queues.
  • Assign Lead: Assign the Lead to the determined user or queue.

Here’s a simplified example of how you can implement this:

apex
Copy code
trigger LeadRoundRobinAssign on Lead (before insert) {
// Retrieve list of users or queues eligible for round-robin assignment
List<User> users = [SELECT Id FROM User WHERE IsActive = true ORDER BY Name ASC LIMIT 5]; // Example query, limit to 5 users
// List<QueueSobject> queues = [SELECT Id FROM QueueSobject WHERE …]; // Example query for queues

// Iterate over the new leads and assign them in round-robin fashion
for (Lead newLead : Trigger.new) {
// Determine the next user or queue to assign the lead to
User nextAssignee = getNextAssignee(users);
// QueueSobject nextAssignee = getNextAssignee(queues);

// Assign the lead to the determined user or queue
if (nextAssignee != null) {
newLead.OwnerId = nextAssignee.Id;
}
}
}

// Method to determine the next assignee in round-robin fashion
private User getNextAssignee(List<User> users) {
// Retrieve the last assigned user from the custom field on the Lead
// Implement logic to determine the next user in round-robin fashion
// For example, you could keep track of the last assigned user’s index and increment it
// Return the next user from the list
// Update the custom field on the Lead with the new assigned user
return users[0]; // Placeholder, return the first user for demonstration
}

Remember to replace the placeholder logic with your actual round-robin assignment logic and error handling. Additionally, you might need to handle scenarios such as when there are no eligible assignees or when the list of assignees changes dynamically.

2) Assigning Leads to Queue or User without coding or Process builder

You can use Lightning Flow to auto assign a new Lead to a Queue or User in Salesforce without coding or Process Builder. Here’s a step-by-step guide:

a. Create a Flow:

    • Navigate to Setup.
    • In the Quick Find box, type “Flows” and select “Flows”.
    • Click the “New Flow” button to create a new Flow.
    • Choose the Screen Flow template and click “Create”.

b. Define Flow Variables:

    • Create any necessary input variables to capture Lead details (e.g., Lead Name, Lead Email, etc.).
    • Create a variable to store the Queue or User ID to which the Lead will be assigned.

c. Build the Flow:

    • Drag and drop the necessary elements onto the canvas to build your Flow. Here’s a basic outline:
      Start element: Begin the Flow.
    • Get Records element: Use this element to query for the Queue or User ID to which you want to assign the Lead. You can use a Record Lookup to find the Queue or User based on specific criteria.
    • Create Records element: Use this element to create the Lead record.
    • Assign Record element: Use this element to assign the newly created Lead record to the Queue or User retrieved in the previous step.
    • End element: End the Flow.

d. Configure Flow Logic:

    • Configure the elements to perform the necessary actions based on your business requirements. For example, you may need to set up decision elements to determine whether the Lead should be assigned to a Queue or User based on certain criteria.

e. Activate the Flow:

    • Once you’ve finished building the Flow, activate it so that it can be used.

f. Implement Flow Triggering:

    • Determine how the Flow will be triggered when a new Lead is created. You can do this by using Process Builder, Workflow Rules, or Record-Triggered Flows.
      For example, if you want the Flow to be triggered when a new Lead record is created, you can create a Process Builder process that launches the Flow whenever a Lead record meets certain criteria.

g. Test the Automation:

    • Create a new Lead record in Salesforce and ensure that it gets automatically assigned to the designated Queue or User based on the Flow logic.

Feel free to reach out to us for anything Salesforce. We love to get technical with our customers. You can write to us at [email protected] or fill up the form, one of us will get back to you.

preloader image