+ Start a Discussion
Deepu sfdcDeepu sfdc 

Apex trigger to assign all incoming leads

Criteria :
If the lead RecordType = ‘US Lead’, then assign the lead to users with country = ‘USA’
If the lead RecordType = ‘International’, then assign the lead to users with Country != ‘USA’
The assignment has to happen in a round-robin fashion. 
For eg., if there are only 3 users(User1,User2,User3) with country = ‘USA’ in the system then the first lead with RecordType = ‘US Lead’ will be assigned to User1, Second lead with RecordType = ‘US Lead’ will be assigned to User2 and so on.
The same logic applies to international leads. 
Workload balance in lead assignment : It would be good if the workload of the user is considered in lead assignment. For eg., User2 is the next in line to get a lead assigned through round-robin fashion and he has 10 open leads and User3 only has 2 open leads assigned. Even though User2 is the next in line, in order to balance the workload of users, the lead can be assigned to user3. 
Best Answer chosen by Deepu sfdc
NagendraNagendra (Salesforce Developers) 
Hi Deepu,

This Round-Robin Lead Assignment solution is simple and powerful. The lead recipient pool is easy to define and manage, and the Management Console view enables you to account for out-of-the-office users and drive lead allocation goals.

Description:How do I randomly assign Leads or Cases to users?

Resolution:To assign leads (or cases) that are either manually created or created using,Web to Lead (or Case).

Follow the instructions below:

The first steps for lead or case assignment is to create two custom fields that can be included in the page layouts for users or hidden as they are strictly for the assignment mechanism based on the custom formula. The instructions below assume the setup is for leads.
1. Create an auto number field in leads called "Lead Number" that spans 0 decimal places and had the format "{0}" (exclude the inverted commas). Below are the steps to create Auto number field:

a) Click on Setup | Customize | Lead | Field.
b) Scroll down and click on “New” button on “Lead Custom Fields & Relationships”.
c) Scroll down and select “Auto Number” radio button.Enter “Field Label” (eg – Lead Number) with that spans 0 decimal places and had the format "{0}" (exclude the inverted commas). 
d) Click on Next for “Field-Level Security for Profile”Click on “Save” to include it different Page Layout Name.

2. Create a custom formula field with return type number in leads called "Round_Robin_ID" and use the formula:
MOD(VALUE(Lead_Number__c) ,3) +1

3. Use the value of this formula field in your lead assignment rules to assign lead records to different queues or users:
Round_Robin_ID = 1 is assigned to Queue A (User 1)
Round_Robin_ID = 2 is assigned to Queue B (User 2)
Round_Robin_ID = 3 is assigned to Queue C (User 3)
Explanation of the process:

The following formula example for leads assumes you have 3 lead queues and you want to assign an equal number of incoming leads to each queue. You can also assign cases using a similar formula.

MOD(VALUE(Lead_Number__c) ,3) +1 is the formula for a custom formula field named Round_Robin_ID that assigns each lead a value of 1, 2, or 3. This formula uses a custom auto-number field called Lead Number that assigns each Lead a sequential number starting with 1. The MOD function divides the lead number by the number of lead queues available (3 in this example) and returns a remainder of 1, 2, or 3.


It will not work on the existing records. In order to make the rule trigger for existing records, you should "Mass" update the records.

If you are creating a record manually, you can check the "Assign using active assignment rule" under options so that the record follows the assignment rule at the time of creation.

For more information check with the below link:

Kindly mark this as solved if it's resolved.

Best Regards,