0

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&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;
    }
}
3
  • 1
    The code you have added here is not the issue. I think you may need to show us the test method that is using this data.
    – Adrian Larson
    Commented Oct 20, 2016 at 15:36
  • Ah, I see you are trying to complete a Trailhead challenge. I think you should simply be using update instead of insert in your Schedulable.
    – Adrian Larson
    Commented Oct 20, 2016 at 15:51
  • @AdrianLarson With 'update' it's ok. Now I want understand why! :D Thanks a lot.
    – vt89
    Commented Oct 20, 2016 at 15:59

1 Answer 1

2

In your schedulable job, you are doing the following:

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

First of all, never use global. Just use public. Second of all, just as the error states, you are not allowed to specify Id in an insert call. The system sets this field when records are created, and it is not writeable.

Take a look at Apex DML Operations to better understand:

Insert Statement

The insert DML operation adds one or more sObjects, such as individual accounts or contacts, to your organization’s data. insert is analogous to the INSERT statement in SQL.

Update Statement

The update DML operation modifies one or more existing sObject records, such as individual accounts or contactsinvoice statements, in your organization’s data. update is analogous to the UPDATE statement in SQL.

Notice that update is the operation intended to modify existing records.

You should change your job to:

public void execute(SchedulableContext sc)
{
    List<Lead> records = [SELECT ID FROM Lead WHERE LeadSource = null LIMIT 200])
    for(Lead record : record)
    {
        // update field value
    }
    update records;
}
2
  • Sorry, but I know that ID is created when the record is on db, not when we create the object.
    – vt89
    Commented Oct 20, 2016 at 18:09
  • You're querying for the the Lead records. They're already in the database, meaning they already have an id. @vt89
    – Adrian Larson
    Commented Oct 20, 2016 at 18:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .