0

I want to insert Account records using batch apex and below is my code . I'm getting following error shown in dev console

5:50:40:090 EXCEPTION_THROWN [18]|System.DmlException: Insert failed. First exception on row 0 with id 0019000000udQWxAAM; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

global class batchAccountInsert implements database.Batchable<sObject> {
    global Database.Querylocator start(Database.BatchableContext bc){

        String query='select Name,Industry from Account';
        return database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext bc , List<Account> scope) {
        List<Account> AccountToInsert = new List<Account>{};
        for(Account a :scope)
        {
        System.debug('Account Industry[' + a.Industry  + '], Name[' + a.name + ']');
        a.Name=a.Name + ' ' + 'Hello' ; 
        AccountToInsert.add(a);
        }
        insert scope;       
   }
   global void finish(Database.BatchableContext bc) {    
}


//Calling the Batch
batchAccountInsert batch = new batchAccountInsert();
Id batchprocessid = Database.executeBatch(batch,200);

Please help .I think while inserting records we should not pass Id ? Am i right ?

6
  • Do you want to update existing records or insert new records here?
    – Manuj
    Jun 19, 2014 at 11:08
  • I want to insert records . But somewhere i'm wrong . Please help me to correct my code.
    – JJoseph
    Jun 19, 2014 at 11:10
  • But you are inserting the same record,which you are querying,if you want to change them ,you should update ,otherwise you would be cloning them to create new records
    – Manuj
    Jun 19, 2014 at 11:13
  • Yes, so how do i achieve that . I want to insert completely new records. I have 19 records now and want to have 200 more records.A total of 219 records. I do not want to clone neither update. If you can please help me to correct my code,it will be very useful for me.
    – JJoseph
    Jun 19, 2014 at 11:17
  • just create new records and insert them ,and they would not be having Id as until a record is not inserted ,Id is not assigned.
    – Manuj
    Jun 19, 2014 at 11:21

2 Answers 2

2
global class batchAccountInsert implements database.Batchable<sObject> {
    global Database.Querylocator start(Database.BatchableContext bc){

        String query='select Name,Industry from Account';
        return database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext bc , List<Account> scope) {
        List<Account> AccountToInsert = new List<Account>{};
        for(Account a :scope)
        {
        Account newAcc = new Account();
        System.debug('Account Industry[' + a.Industry  + '], Name[' + a.name + ']');
        newAcc.Name=a.Name + ' ' + 'Hello' ;
        newAcc.Industry =a.Industry; 
        AccountToInsert.add(newAcc);
        }
        insert AccountToInsert;       
   }
   global void finish(Database.BatchableContext bc) {    
}

You should not be inserting scope here,instead new list created should be inserted.

3
  • Still no luck and getting the same error.:( . really stucked.
    – JJoseph
    Jun 19, 2014 at 11:53
  • I edited my answer,this should work
    – Manuj
    Jun 19, 2014 at 12:03
  • Bingo ! This is working now.Appreciate your knowledge.
    – JJoseph
    Jun 19, 2014 at 12:17
0

The insert dml opration is to create new records in the database. As you're starting with a query of records from the database, you want to use to update dml operation to update those records in the database. The error you're receiving is rather self-explanatory, the insert call won't accept records which already have a populated id field.

changing insert scope; to update scope; should be enough.

Chek out the Apex Data Manipulation Language (DML) Operations Documentation.

3
  • I know i'm wrong here though i have fired select query. Could you please help to make this batch to insert records ?
    – JJoseph
    Jun 19, 2014 at 10:48
  • I'm new to this and banging my head on this from hours.Want to insert records.
    – JJoseph
    Jun 19, 2014 at 11:01
  • You're gonna need to update with more context of what you're trying to achieve for any answer to truly contain what you want. Jun 19, 2014 at 12:06

You must log in to answer this question.

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