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
Mark GallagherMark Gallagher 

Update conditional results

Wrote a trigger that caps how many of a certain type of lead that a user can have need help writing the test class

TestFullCapStatus and TestFullCapType are woriking properly, but the rest aren't and I don't understand why?
TestFullCapTrue shouldn't allow any of the leads to update and the others should allow all the leads under their respective caps to change and reject the rest. Each List<Lead> has 500 so for example TestCap100True should allow the first 100 to change leadOwner, but not the next 400.

<--- Trigger--->
trigger Limit100Leads on Lead (before update)  
{
       for(Lead ld: Trigger.New)
    {  
        Id 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 OwnerId FROM Lead WHERE OwnerId =: LeadOwner AND Lead_Source_Type__c =: 'Team Lead'];
            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.');
            }  
        }
    }
}

<---Test Class--->

@isTest
private class TestLimitData

    @isTest static void TestFullCap()
    {
        User[] MarkId =  [SELECT Id FROM User WHERE Full_Name__c = 'Mark Gallagher']; 
        Id Mark = MarkId[0].Id;
       
        List<Lead> trueLead = TestLimitDataFactory.trueLeads();
        
        for(Lead ld: trueLead)
        {
            ld.OwnerId = Mark;
        }
        
        Test.startTest();
        Database.SaveResult[] results = Database.update(trueLead, true);
        Test.stopTest();
        
        for(Database.SaveResult res : results)
        {
            System.assert(!res.isSuccess());
        }
    }
    
    @isTest static void TestFullCapStatus()
    {
        User[] MarkId =  [SELECT Id FROM User WHERE Full_Name__c = 'Mark Gallagher']; 
        Id Mark = MarkId[0].Id;
       
        List<Lead> statusLead = TestLimitDataFactory.statusLeads();
        
        for(Lead ld: statusLead)
        {
            ld.OwnerId = Mark;
        }
        
        Test.startTest();
        Database.SaveResult[] results = Database.update(statusLead,false);
        Test.stopTest();
        
        for(Database.SaveResult res : results)
        {
            System.assert(res.isSuccess());
        }
    }
    
    @isTest static void TestFullCapType()
    {
        User[] MarkId =  [SELECT Id FROM User WHERE Full_Name__c = 'Mark Gallagher']; 
        Id Mark = MarkId[0].Id;
       
        List<Lead> typeLead = TestLimitDataFactory.typeLeads();
        
        for(Lead ld: typeLead)
        {
            ld.OwnerId = Mark;
        }
        
        Test.startTest();
        Database.SaveResult[] results = Database.update(typeLead,false);
        Test.stopTest();
        
        for(Database.SaveResult res : results)
        {
            System.assert(res.isSuccess());
        }
    }
    
    @isTest static void TestCap100True()
    {
        User[] cap100Id =  [SELECT Pipeline_Cap__c FROM User WHERE Full_Name__c = 'Jayte Acree']; 
        Id cap100 = cap100Id[0].Id;
        Double cap = cap100Id[0].Pipeline_Cap__c;
        
       
        List<Lead> trueLead = TestLimitDataFactory.trueLeads();
        
        for(Lead ld: trueLead)
        {
            ld.OwnerId = cap100;
        }
        
        Test.startTest();
        Database.SaveResult[] results = Database.update(trueLead,false);
        Test.stopTest();
        
        for(Integer i = 0; i < results.size(); i++)
        {
            if(i < cap)
            {
                System.assert(results[i].isSuccess());
            }
            else
            {
                System.assert(!results[i].isSuccess());
            }
        }
    }
    
    @isTest static void TestCap200True()
    {
        User[] cap200Id =  [SELECT Pipeline_Cap__c FROM User WHERE Full_Name__c = 'Sandra Alvidrez']; 
        Id cap200 = cap200Id[0].Id;
        Double cap = cap200Id[0].Pipeline_Cap__c;
        
       
        List<Lead> trueLead = TestLimitDataFactory.trueLeads();
        
        for(Lead ld: trueLead)
        {
            ld.OwnerId = cap200;
        }
        
        Test.startTest();
        Database.SaveResult[] results = Database.update(trueLead,false);
        Test.stopTest();
        
        for(Integer i = 0; i < results.size(); i++)
        {
            if(i < cap)
            {
                System.assert(results[i].isSuccess());
            }
            else
            {
                System.assert(!results[i].isSuccess());
            }
        }
    }
    
    @isTest static void TestCap300True()
    {
        User[] cap300Id =  [SELECT Pipeline_Cap__c FROM User WHERE Full_Name__c = 'Johnny App']; 
        Id cap300 = cap300Id[0].Id;
        Double cap = cap300Id[0].Pipeline_Cap__c;
        
       
        List<Lead> trueLead = TestLimitDataFactory.trueLeads();
        
        for(Lead ld: trueLead)
        {
            ld.OwnerId = cap300;
        }
        
        Test.startTest();
        Database.SaveResult[] results = Database.update(trueLead,false);
        Test.stopTest();
        
        for(Integer i = 0; i < results.size(); i++)
        {
            if(i < cap)
            {
                System.assert(results[i].isSuccess());
            }
            else
            {
                System.assert(!results[i].isSuccess());
            }
        }
    }
    
    @isTest static void TestCap400True()
    {
        User[] cap400Id =  [SELECT Pipeline_Cap__c FROM User WHERE Full_Name__c = 'Allison Arevalo']; 
        Id cap400 = cap400Id[0].Id;
        Double cap = cap400Id[0].Pipeline_Cap__c;
        
       
        List<Lead> trueLead = TestLimitDataFactory.trueLeads();
        
        for(Lead ld: trueLead)
        {
            ld.OwnerId = cap400;
        }
        
        Test.startTest();
        Database.SaveResult[] results = Database.update(trueLead,false);
        Test.stopTest();
        
        for(Integer i = 0; i < results.size(); i++)
        {
            if(i < cap)
            {
                System.assert(results[i].isSuccess());
            }
            else
            {
                System.assert(!results[i].isSuccess());
            }
        }
    }
}