Skip to main content
added 1344 characters in body
Source Link
Matt Lacey
  • 25.8k
  • 7
  • 68
  • 153

Why?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;
    }
}

Why?

Why?

The complete test class is here:

@isTest
private class DailyLeadProcessorTest {
    @testSetup
    public static void setupMethod(){
        List&lt;Lead&gt; l = new List&lt;Lead&gt;();
        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;
    }
}
edited tags
Link
Adrian Larson
  • 151.3k
  • 38
  • 247
  • 431
added 25 characters in body
Source Link
Adrian Larson
  • 151.3k
  • 38
  • 247
  • 431

I have this class:

@isTest private class DailyLeadProcessorTest { @testSetup public static void setupMethod(){ List<Lead> l = new List<Lead>(); for(Integer i=0; i
@isTest
private class DailyLeadProcessorTest {
    @testSetup
    public static void setupMethod(){
        List&lt;Lead&gt; l = new List&lt;Lead&gt;();
        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]'

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?

I have this class:

@isTest private class DailyLeadProcessorTest { @testSetup public static void setupMethod(){ List<Lead> l = new List<Lead>(); for(Integer i=0; i

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?

I have this class:

@isTest
private class DailyLeadProcessorTest {
    @testSetup
    public static void setupMethod(){
        List&lt;Lead&gt; l = new List&lt;Lead&gt;();
        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?

Source Link
vt89
  • 725
  • 12
  • 26
Loading