I have this class:
@isTest
private class DailyLeadProcessorTest {
@testSetup
public static void setupMethod(){
List<Lead> l = new List<Lead>();
for(Integer i=0; i<1; i++)
l.add(new Lead(LastName='LastName Company: ' + i, Company='Company Name: ' + i));
insert l;
}
}
When I execute the Test, I have this error:
FATAL_ERROR System.DmlException: Insert failed. First exception on row 0 with id 00Q58000006DY7WEAW; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
Why?
The complete test class is here:
@isTest
private class DailyLeadProcessorTest {
@testSetup
public static void setupMethod(){
List<Lead> l = new List<Lead>();
for(Integer i=0; i<1; i++)
l.add(new Lead(LastName='LastName Company: ' + i, Company='Company Name: ' + i));
insert l;
}
@isTest
public static void test_method_one() {
Test.startTest();
DailyLeadProcessor dlp = new DailyLeadProcessor();
String sch = '20 30 8 10 2 ?';
String jobID = System.schedule('Remind Opp Owners', sch, dlp);
Test.stopTest();
}
}
While the class tested is here:
global class DailyLeadProcessor implements Schedulable {
/*
The execute method must find the first 200 Leads with a blank LeadSource
field and update them with the LeadSource value of 'Dreamforce'.
*/
global void execute(SchedulableContext sc) {
List<Lead> newListLead = new List<Lead>();
for(List<Lead> l: [SELECT ID FROM Lead WHERE LeadSource = null LIMIT 200])
for(Lead elem : l){
Lead newLead = elem;
newLead.LeadSource = 'Dreamforce';
newListLead.add(newLead);
}
insert newListLead;
}
}
update
instead ofinsert
in yourSchedulable
.