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
Sree07Sree07 

Create more than 50,000 records and update a field

I want to create more than 50,000 records in account object using code.Later I want to update Rating field to 'Warm' to all the records in present in account object.
VICKY_SFDCVICKY_SFDC
IF MY SOLUTION HELPS YOU,,, MY ANSWER AS BEST ANSWER 


STEP 2
cntrl+E(Anonymisis Window)==>PASTE BELOW CODE
Batch1 be=new Batch1();
Database.executeBatch(be);


STEP 1)PASTE BELOW CODE

global class Batch1 implements Database.Batchable<Sobject>{
 
    //Method to get the data to be proceesed  
    global database.Querylocator Start(Database.BatchableContext bc){
        String query = 'Select Id, Name,Rating  From Account ';
        return Database.getQueryLocator(query);
    }
 
 
    //Method to execute the batch
    global void execute(Database.BatchableContext bc, List<Account> scope){
       
        for(Account a : scope){
       a.Rating ='Warm';
            
        }
         update scope;
    }
 
    //Method to be called after the excute
    global void finish(Database.BatchableContext bc){
       
        AsyncApexJob jb=[SELECT CompletedDate,JobItemsProcessed,JobType,LastProcessed,NumberOfErrors,ParentJobId,Status,TotalJobItems FROM AsyncApexJob where id=:bc.getJobId()];
        Messaging.SingleEmailMessage msg=new Messaging.SingleEmailMessage();
        String[] toadd=new String[]{'YOUREMAILID@gmail.com'};//YOU CAN GIVE ANY EMAIL ID HERE
            msg.setToAddresses(toadd);
        String body='Dear Admin,<br/>Batch Operation Of JobId:<b>'+jb.id;
        body=body+'</b><br/><br/>Processes Successfully<br/>Total JobItems:<b>'+jb.TotalJobItems;
        body=body+'</b><br/><br/>No of Errors :<b>'+jb.NumberOfErrors+'</b>';
        msg.setSubject('Batch Status');
        msg.setHtmlbody(body);
        Messaging.Email[] emails=new Messaging.Email[]{msg};
        Messaging.sendEmail(emails);

        //Add your start code for the other batch job here
        //Database.executeBatch(new Batch2());
       
    }
}
Sree07Sree07
I tried with this code.It is showing an error like:
First error: Too many DML rows: 10001

global class BatchAccounts implements Database.Batchable<sObject>{
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT Id,Name FROM Account'  ;         
        return Database.getQueryLocator(query);       
    }
    
    global void execute(Database.BatchableContext BC, List<Account> scope) {
        List<Account> accList = new List<Account>();
        for (integer i=1;i<60000;i++)
        {
            Account anew= new Account(Name ='Account '+i);
            anew.Rating = 'Warm';
            accList.add(anew); 
        }
            upsert accList; 
    }
    global void finish(Database.BatchableContext BC) {
    }
}