+ Start a Discussion
Mark GallagherMark Gallagher 

Trigger not firing

This trigger isn't firing and I can't figure out why help please

<--Trigger Code--->

trigger LimitLeads on Lead (before update) 
{
    for(Lead ld: Trigger.New)
    {  
        String LeadOwner = ld.OwnerId;
        if(ld.Lead_Source_Type__c == 'Team Lead' &&  (ld.Status == '01-New' && ld.Status == '20-Nurture') && ld.RecordTypeId == '01250000000Mvl2AAC')
        {
            List<Lead> leads = [SELECT Id FROM Lead WHERE OwnerId =: LeadOwner AND Lead_Source_Type__c =: 'Team Lead' AND (Status = '01-New' OR Status = '20-Nurture') AND RecordTypeId = '01250000000Mvl2AAC'];
            List<User> caps  = [SELECT Pipeline_Cap__c FROM User WHERE Id =: LeadOwner];
            Integer numLeads = leads.size();
            Integer cap = (Integer)caps[0].Pipeline_Cap__c;
            Integer remove = leads.size() - cap + 1;
                                              
            If(numLeads > cap) 
            {
                ld.OwnerId.addError('You already have too many Team Leads. Please remove leads before adding a new one.');
            }  
        
        }
    }    
}
Maharajan CMaharajan C
Hi Mark,

Change the if Condition:

        if(ld.Lead_Source_Type__c == 'Team Lead' &&  (ld.Status == '01-New' || ld.Status == '20-Nurture') && ld.RecordTypeId == '01250000000Mvl2AAC')

============

or try the below:

If(ld.Lead_Source_Type__c =: 'Team Lead' AND (ld.Status = '01-New' OR ld.Status = '20-Nurture') AND ld.RecordTypeId = '01250000000Mvl2AAC')

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!

Thanks,
Raj
 
Mark GallagherMark Gallagher
No that didn't work I believe that those words only work as logic operators for SOQL 
Nishant Prajapati 10Nishant Prajapati 10
Hi Mark, Try following code. 
trigger LimitLeads on Lead (before insert, before update) 
{   
    set<Id> userIdSet = new set<Id>();
    for(Lead ld: Trigger.New){
        Boolean cond = ld.Lead_Source_Type__c == 'Team Lead' &&  (ld.Status == '01-New' || ld.Status == '20-Nurture') && ld.RecordTypeId == '01250000000Mvl2AAC';
        if(cond)
            userIdSet.add(ld.OwnerId);
    }
    if(userIdSet.size() > 0){
        Map<Id, User> userCapMAP = new Map<Id, User>([select id, Pipeline_Cap__c from user where id IN: userIdSet]);
        if(userCapMAP.size() > 0){
            AggregateResult[]   groupedResults = [select count(Id), ownerId from Lead GROUP BY ownerId];
            for(Lead le : trigger.newmap.values()){
                for (AggregateResult ar : groupedResults)  {
                    if(userCapMAP.containskey(le.ownerId) && le.ownerId == ar.get('ownerId')){
                        if((Integer)ar.get('expr0') > userCapMAP.get(le.ownerId).Pipeline_Cap__c)    
                            le.addError('You already have too many Team Leads. Please remove leads before adding a new one.');
                    }    
                }    
            }
                        
        } 
    }
}

 
Raj VakatiRaj Vakati
This might be the issues 
  1. Record type Id might be not correct .. please check one more time 
  2. Make sure your trigger is active

updated Code is here 
 
trigger LimitLeads on Lead (before update) 
{
    for(Lead ld: Trigger.New)
    {  
        String LeadOwner = ld.OwnerId;
        if(ld.Lead_Source_Type__c == 'Team Lead' &&
		(ld.Status == '01-New' && ld.Status == '20-Nurture') && ld.RecordTypeId == '01250000000Mvl2AAC')
        {
            List<Lead> leads = [SELECT Id FROM Lead WHERE OwnerId =: LeadOwner AND Lead_Source_Type__c ='Team Lead' AND 
			(Status = '01-New' OR Status = '20-Nurture') AND RecordTypeId = '01250000000Mvl2AAC'];
            List<User> caps  = [SELECT Pipeline_Cap__c FROM User WHERE Id =: LeadOwner];
            Integer numLeads = leads.size();
            Integer cap = (Integer)caps[0].Pipeline_Cap__c;
            Integer remove = leads.size() - cap + 1;
                                              
            If(numLeads > cap) 
            {
                ld.OwnerId.addError('You already have too many Team Leads. Please remove leads before adding a new one.');
            }  
        
        }
    }    
}