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 

invaild field for insert update

Wrote a trigger to limit the amount of leads a specific owner can have. Trying to write the test class for the trigger and am getting this error:
System.DmlException: Insert failed. First exception on row 0 with id a0O17000001aWCWEA2; first error: INVALID_FIELD_FOR_INSERT_UPDATE
<--- Trigger Code --->
trigger Limit100Leads on Lead (before insert, 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 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 + 2;
            
                                              
            If(numLeads > cap) 
            {
                ld.OwnerId.addError('You already have '+ leads.size() + ' Team Leads. Please remove ' + remove + ' before adding a new one.');
            }  
        }
    }
}

<--- Test Class Code --->
@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(Mark);
        
        Test.startTest();
        Database.SaveResult[] results = Database.insert(trueLead, false);
        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(Mark);
        
        Test.startTest();
        Database.SaveResult[] results = Database.insert(statusLead, true);
        Test.stopTest();
        
        for(Database.SaveResult res : results)
        {
            System.assert(res.isSuccess());
        }
    }
}
Best Answer chosen by Mark Gallagher
Steven NsubugaSteven Nsubuga
The issue is that within your TestLimitDataFactory, you insert trueLeads and statusLeads.
Then in your test methods in TestLimitData, you do an insert on those same records which you just inserted in the TestLimitDataFactory.
That is why Database.insert(statusLeads,false) fails while Database.update(statusLeads,false) works.
@isTest
public class TestLimitDataFactory 
{
    public static List<Lead> trueLeads(Id OwnerId)
    {
         List<Lead> trueLeads = new List<Lead>();
           for(Integer i = 0; i < 500; i++)
        {
            trueLeads.add(new Lead(LastName = 'trueTest' + i, Lead_Source_Type__c = 'Team Lead', Status = '01-New', RecordTypeId = '01250000000Mvl2AAC', OwnerId = OwnerId));
        }
               
        return trueLeads;
    }
    
    public static List<Lead> statusLeads(Id OwnerId)
    {
         List<Lead> statusLeads = new List<Lead>();
           for(Integer i = 0; i < 500; i++)
        {
               statusLeads.add(new Lead(LastName = 'statusTest' + i, Status = '90-Trash', OwnerId = OwnerId));
        }
        
        return statusLeads;
    }
    
    public static List<Lead> recordLeads(Id OwnerId)
    {
         List<Lead> recordLeads = new List<Lead>();
           for(Integer i = 0; i < 500; i++)
        {
               recordLeads.add(new Lead(LastName = 'recordTest' + i, RecordTypeId = '0120d0000001GzlAAE', OwnerId = OwnerId));
        }
        insert recordLeads;
        return recordLeads;
    }
    
    public static List<Lead> typeLeads(Id OwnerId)
    {
         List<Lead> typeLeads = new List<Lead>();
           for(Integer i = 0; i < 500; i++)
        {
               typeLeads.add(new Lead(LastName = 'typeTest' + i, Lead_Source_Type__c = 'Non-Team Lead', OwnerId = OwnerId));
        }
        insert typeLeads;
        return typeLeads;
    }
}
I have removed the insert calls from the trueLeads and statusLeads methods in the TestLimitDataFactory. You might want to remove the rest of the insert calls from the other methods in the TestLimitDataFactory.
Switching back to Database.insert(statusLeads,false) should work fine now.
 

All Answers

Steven NsubugaSteven Nsubuga
Something inside your TestLimitDataFactory.statusLeads(Mark) method is causing this.
Are you trying to set the CreatedById field? If so then that is the problem.
Mark GallagherMark Gallagher
I am not trying to set the CreatedById I'm trying to set the OwnerId field
<--- Status Leads Method --->

  public static List<Lead> statusLeads(Id OwnerId)
    {
         List<Lead> statusLeads = new List<Lead>();
           for(Integer i = 0; i < 500; i++)
        {
               statusLeads.add(new Lead(LastName = 'statusTest' + i, Status = '90-Trash', OwnerId = OwnerId));
        }
        insert statusLeads;
        return statusLeads;
    }
Steven NsubugaSteven Nsubuga
What about  inside TestLimitDataFactory.trueLeads(Mark)?
Mark GallagherMark Gallagher
I switched the stament Database.SaveResult[] results = Database.insert(statusLeads,false); to
                                     Database.SaveResult[] results = Database.update(statusLeads,false); 
I the test completed, but I don't want to test updating leads I want to test inserting new leads.
<--- Test Limit Data Factory Class --->

@isTest
public class TestLimitDataFactory 
{
    public static List<Lead> trueLeads(Id OwnerId)
    {
         List<Lead> trueLeads = new List<Lead>();
           for(Integer i = 0; i < 500; i++)
        {
            trueLeads.add(new Lead(LastName = 'trueTest' + i, Lead_Source_Type__c = 'Team Lead', Status = '01-New', RecordTypeId = '01250000000Mvl2AAC', OwnerId = OwnerId));
        }
        insert trueLeads;
        
        return trueLeads;
    }
    
    public static List<Lead> statusLeads(Id OwnerId)
    {
         List<Lead> statusLeads = new List<Lead>();
           for(Integer i = 0; i < 500; i++)
        {
               statusLeads.add(new Lead(LastName = 'statusTest' + i, Status = '90-Trash', OwnerId = OwnerId));
        }
        insert statusLeads;
        return statusLeads;
    }
    
    public static List<Lead> recordLeads(Id OwnerId)
    {
         List<Lead> recordLeads = new List<Lead>();
           for(Integer i = 0; i < 500; i++)
        {
               recordLeads.add(new Lead(LastName = 'recordTest' + i, RecordTypeId = '0120d0000001GzlAAE', OwnerId = OwnerId));
        }
        insert recordLeads;
        return recordLeads;
    }
    
    public static List<Lead> typeLeads(Id OwnerId)
    {
         List<Lead> typeLeads = new List<Lead>();
           for(Integer i = 0; i < 500; i++)
        {
               typeLeads.add(new Lead(LastName = 'typeTest' + i, Lead_Source_Type__c = 'Non-Team Lead', OwnerId = OwnerId));
        }
        insert typeLeads;
        return typeLeads;
    }
}
Steven NsubugaSteven Nsubuga
The issue is that within your TestLimitDataFactory, you insert trueLeads and statusLeads.
Then in your test methods in TestLimitData, you do an insert on those same records which you just inserted in the TestLimitDataFactory.
That is why Database.insert(statusLeads,false) fails while Database.update(statusLeads,false) works.
@isTest
public class TestLimitDataFactory 
{
    public static List<Lead> trueLeads(Id OwnerId)
    {
         List<Lead> trueLeads = new List<Lead>();
           for(Integer i = 0; i < 500; i++)
        {
            trueLeads.add(new Lead(LastName = 'trueTest' + i, Lead_Source_Type__c = 'Team Lead', Status = '01-New', RecordTypeId = '01250000000Mvl2AAC', OwnerId = OwnerId));
        }
               
        return trueLeads;
    }
    
    public static List<Lead> statusLeads(Id OwnerId)
    {
         List<Lead> statusLeads = new List<Lead>();
           for(Integer i = 0; i < 500; i++)
        {
               statusLeads.add(new Lead(LastName = 'statusTest' + i, Status = '90-Trash', OwnerId = OwnerId));
        }
        
        return statusLeads;
    }
    
    public static List<Lead> recordLeads(Id OwnerId)
    {
         List<Lead> recordLeads = new List<Lead>();
           for(Integer i = 0; i < 500; i++)
        {
               recordLeads.add(new Lead(LastName = 'recordTest' + i, RecordTypeId = '0120d0000001GzlAAE', OwnerId = OwnerId));
        }
        insert recordLeads;
        return recordLeads;
    }
    
    public static List<Lead> typeLeads(Id OwnerId)
    {
         List<Lead> typeLeads = new List<Lead>();
           for(Integer i = 0; i < 500; i++)
        {
               typeLeads.add(new Lead(LastName = 'typeTest' + i, Lead_Source_Type__c = 'Non-Team Lead', OwnerId = OwnerId));
        }
        insert typeLeads;
        return typeLeads;
    }
}
I have removed the insert calls from the trueLeads and statusLeads methods in the TestLimitDataFactory. You might want to remove the rest of the insert calls from the other methods in the TestLimitDataFactory.
Switching back to Database.insert(statusLeads,false) should work fine now.
 
This was selected as the best answer