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 

Trigger not allowing Lead ownership change

Hi Everyone, 

So we got this trigger from a external source and we now notice that it is working well as expected that the trigger is not allowing to change ownership of lead records for all users. 

Is there a way we can add one more logic in this excisting code to allow all user to be able to change ownership of lead records, please?

Below is the code
trigger LimitLeads on Lead ( before update )
{
    Integer maxLeads = 1;
    Decimal leadsCount;
    
    Map<Id,List<Lead>> leadsByNewOwnerId = new Map<Id,List<Lead>>();
    Map<Id,List<String>> SelectfrmQueueMap = new Map<Id,List<String>>();
    Map<Id,List<String>> LeadfrmQueueMap1 = new Map<Id,List<String>>();
    Map<Id,List<String>> LeadfrmQueueMap2 = new Map<Id,List<String>>();
    
    List<String> emailSelectList = New  List<String>();
    List<Lead> LeadList = new 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, LeadList );
                SelectfrmQueueMap.put( lead.Id,emailSelectList);
            }
            LeadList.add(lead);
            emailSelectList.add(lead.Email);
        }
    }
  
    System.debug('leadsByNewOwnerId Size : ' + LeadList.size());
   
   List<Lead> LeadfrmQueueList1 = new List<Lead>([Select Id,Name,CreatedDate,Email,OwnerId from Lead where (OwnerId = '00G1C000004b97J') order by createdDate ASC limit 1 ]);
   List<Lead> LeadfrmQueueList2 = new List<Lead>([Select Id,Name,CreatedDate,Email,OwnerId from Lead where (OwnerId = '00G1C000004b97E') order by createdDate ASC limit 1 ]);
   
   If(LeadfrmQueueList1.size() > 0)
   {
    List<String> emailList = New  List<String>();
    emailList.add(LeadfrmQueueList1[0].Email);
   LeadfrmQueueMap1.put(LeadfrmQueueList1[0].Id, emailList);
   }
   
   If(LeadfrmQueueList2.size() > 0)
   {
    List<String> emailList = New  List<String>();
    emailList.add(LeadfrmQueueList2[0].Email);
   LeadfrmQueueMap2.put(LeadfrmQueueList2[0].Id, emailList);
   }

System.debug('SelectfrmQueueMap' + SelectfrmQueueMap);
System.debug('LeadfrmQueueMap1' + LeadfrmQueueMap1);
System.debug('LeadfrmQueueMap2' + LeadfrmQueueMap2);

Set<Id> userIds = new Set<Id>{'00515000006MBtJ', '00515000005c12P','00515000006MBtE'} ;

If(LeadList.size() > 0)
{
If(!(userIds.contains(LeadList[0].OwnerId)))
{

    If(!(SelectfrmQueueMap.equals(LeadfrmQueueMap1) || SelectfrmQueueMap.equals(LeadfrmQueueMap2)))
    {
        System.debug('Enter inside');
        for ( Lead lead : LeadList )
        {
            System.debug('Lead Before 11:' + lead );
            lead.addError (   'Users cannot have more than '+   maxLeads +   ' "Open - Not Contacted" leads.'  );
        }
    }
    else If(LeadList.size() > maxLeads )
    {
      System.debug('Enter inside loop 2');
        for ( Lead lead : LeadList )
        {
            System.debug('Lead Before 22:' + lead );
            lead.addError (   'Users cannot have more than '+   maxLeads +   ' "Open - Not Contacted" leads.'  );
        }
       
    }
    else
    {
        System.debug('Enter inside loop 3');
       for (AggregateResult result : [SELECT COUNT(Id) cid, OwnerId FROM Lead WHERE(Status = '1 Uncontacted' AND OwnerId IN :leadsByNewOwnerId.keySet()) GROUP BY OwnerId ])
        {
            System.debug('result==>' + ((decimal)result.get('cid')));
            Decimal cidsize = ((decimal)result.get('cid'));
            leadsCount = cidsize + LeadList.size();
            if(leadsCount > maxLeads)
            {
                 
                System.debug('Test:' + leadsByNewOwnerId.get( (Id) result.get( 'OwnerId' ) ) );
                for ( Lead lead : leadsByNewOwnerId.get( (Id) result.get( 'OwnerId' ) ) )
                {
                   System.debug('Lead After:' + lead );
                   lead.addError (   'Users cannot have more than '+   maxLeads +   ' "Open - Not Contacted" leads.'  );
                }
                 
            }
        }
    }
    
     System.debug('Exit');
}
}
}