You need to sign in to do that
Don't have an account?
Mark 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());
}
}
}
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());
}
}
}
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. 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
Are you trying to set the CreatedById field? If so then that is the problem.
<--- 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;
}
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;
}
}
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. 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.