You need to sign in to do that
Don't have an account?
Sha 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)
{
}
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)
{
}
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
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'.
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