1
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) {
    }
}

here return database.getQueryLocator(query) can return total records which are present in Account object, so only that number of new records can be created, what if I want to create more than 20K new records. what should be the initialization in start method for this?

1 Answer 1

2

You can control the number of records that are passed to any one invocation of the execute method using the scope when you start the batch job.

See Database.executeBatch(object, integer)

scope
Type: Integer
Number of records to be passed into the execute method for batch processing.

And Using the Database.executeBatch Method to Submit Batch Jobs

An optional parameter scope. This parameter specifies the number of records that should be passed into the execute method. Use this parameter when you have many operations for each record being passed in and are running into governor limits. By limiting the number of records, you are thereby limiting the operations per transaction. This value must be greater than zero. If the start method of the batch class returns a QueryLocator, the optional scope parameter of Database.executeBatch can have a maximum value of 2,000. If set to a higher value, Salesforce chunks the records returned by the QueryLocator into smaller batches of up to 2,000 records. If the start method of the batch class returns an iterable, the scope parameter value has no upper limit; however, if you use a very high number, you may run into other limits.

By decreasing the scope size you can stay within the DML limits for inserting new Accounts.

You must log in to answer this question.

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