function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sha 8Sha 8 

how to insert records through batch apex in object having autonumber field

i want to insert records through apex batch class in an object having auto number field for one column in the object

i am getting the below error message
First error: Insert failed. First exception on row 0 with id a049000000EoeVXAAZ; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

below is my batch apex code:
global class batchapexexample implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'SELECT  Account__c,templ__c,Amount__c  FROM Balances__c';
        return Database.getQueryLocator(query);
    }
  
    global void execute(Database.BatchableContext BC, List<balances__c>scope)
    {
      insert scope;
    }  
    global void finish(Database.BatchableContext BC)
    {
       
    }

Best Answer chosen by Sha 8
Boris BachovskiBoris Bachovski
You can't insert a record that already exists. In your batch you're querying records that already exist in the database and you're trying to re-insert them again. You migth want to 'update' them instead.

You can still insert a new record like this:

List <Balances__c> balancesToInsert = new List <Balances__c> ();

Balances__c blance = new Balances__c ();
balance.Account__c = someAccountId;
// set other fields
balancesToInsert.add(balance);

// do the same if you need to create multiple records
// and finally

insert balancesToInsert();


In case you're not sure if it's an existing record or not, you can use the keyword 'upsert'.

All Answers

Boris BachovskiBoris Bachovski
You can't insert a record that already exists. In your batch you're querying records that already exist in the database and you're trying to re-insert them again. You migth want to 'update' them instead.

You can still insert a new record like this:

List <Balances__c> balancesToInsert = new List <Balances__c> ();

Balances__c blance = new Balances__c ();
balance.Account__c = someAccountId;
// set other fields
balancesToInsert.add(balance);

// do the same if you need to create multiple records
// and finally

insert balancesToInsert();


In case you're not sure if it's an existing record or not, you can use the keyword 'upsert'.
This was selected as the best answer
Sha 8Sha 8
Hi Boris Bachovski,

Thanks for your reply.

i have one more doubt can i show message popups(status of batch whether batch as successfully completed or error in inserting) in final method instead of sending emails
Boris BachovskiBoris Bachovski
You can't have real-time messages happening as the batch is executing asynchroniously. You can either do a 'system.debug' and monitor the user who ran that batch then check the debug logs, or alternatively, you can create a custom object called "Api Log" or something, and for each batch in the finish method create a new "Api Log" record. That way you can record time of execution, errors and anything else that you like to record. Keep in mind that if you decide to do custom logging, you'll need another batch that will remove old logs that have created date < today - X days.