function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Domnic JohnsonDomnic Johnson 

Can we limit users from taking any leads from queue when they have uncontacted leads?

Hi All, 

I want to have leads be able to take x number of leads from the queue at a time and the next time they want to take they should not be having any uncontacted leads which are coming from the queue.

Please help
SandhyaSandhya (Salesforce Developers) 
Hi,

You may try to Set Up Assignment Rules for leads.

Refer below salesforce help article

https://help.salesforce.com/articleView?id=creating_assignment_rules.htm&type=5
 
Best Regards,
Sandhya
Domnic JohnsonDomnic Johnson
Hi Sandhya, 

I want to limit the user from accepting any leads when a user has any open leads

Not sure if I can use assignment rule here.

Thanks for responding, 
Domnic
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
Domnic, The following trigger finds leads whose owner has changed, then adds an error if the owner has too many open leads:

<pre>
trigger LimitLeads on Lead ( before update )
{
    Integer maxLeads = 4;

    Map<Id,List<Lead>> leadsByNewOwnerId = new Map<Id,List<Lead>>();
    for ( Lead lead : Trigger.new )
    {
        Lead oldLead = Trigger.oldMap.get( lead.Id );
        if  (   oldLead.OwnerId != lead.OwnerId
            &&  String.valueOf( lead.OwnerId ).left( 3 ) == '005'
            )
        {
            if ( !leadsByNewOwnerId.containsKey( lead.OwnerId ) )
            {
                leadsByNewOwnerId.put( lead.OwnerId, new List<Lead>() );
            }
            leadsByNewOwnerId.get( lead.OwnerId ).add( lead );
        }
    }

    for ( AggregateResult result :
        [   SELECT  COUNT(Id), OwnerId
            FROM    Lead
            WHERE   (   Status = 'Open - Not Contacted'
                    AND OwnerId IN :leadsByNewOwnerId.keySet()
                    )
            GROUP BY OwnerId
            HAVING  COUNT(Id) >= :maxLeads
        ]
        )
    {
        for ( Lead lead : leadsByNewOwnerId.get( (Id) result.get( 'OwnerId' ) ) )
        {
            lead.addError
            (   'Users cannot have more than '
            +   maxLeads
            +   ' "Open - Not Contacted" leads.'
            );
        }
    }
}
</pre>
Domnic JohnsonDomnic Johnson
Hi Glyn, 

Thanks for the code. So, whats happening is if the user has more than 4 or more uncontacted leads the user is not able to accept any leads from the queue. But if the user has less than 3 uncontacted leads, the user is able to accept any number of leads from the queue. 

Is there a way to limit the number of leads the user can accept from the queue?

Thanks again,
Domnic