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
Soubhagya Ranjan 2Soubhagya Ranjan 2 

Batch Apex Code

Hi All ,
 I have folowing requirement .

i need to update account name through batch apex . if there is no error then update the name . and if there is some error then update the Status__c field as NO and send email to user with attached CSV file with the list of errors .

below is the code where i am able to send the eroor as csv file but not able to upadte the status field 

global class batchAccount2 implements Database.Batchable<sobject>, Database.stateful {
  global Map<Id, String> errorMap {get; set;}
    global Map<Id, SObject> IdToSObjectMap {get; set;}
    global batchAccount2 (){
        errorMap = new Map<Id, String>();
        IdToSObjectMap = new Map<Id, 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> accountList = new List<Account>();
        for(Account a : scope) {
            a.Name = a.Name + 'Updated';
        if(accountList.size() > 0) {
            List<Database.SaveResult> dsrs = Database.Update(accountList, false);
            Integer index = 0;
            for(Database.SaveResult dsr : dsrs){
                String errMsg = dsr.getErrors()[0].getMessage();
                    errorMap.put(accountList[index].Id, errMsg);
                    IdToSObjectMap.put(accountList[index].Id, accountList[index]);
    global void finish(Database.BatchableContext BC) { 
       //Send an email to the User after your batch completes 
            AsyncApexJob a = [SELECT id, ApexClassId,
                       JobItemsProcessed, TotalJobItems,
                       NumberOfErrors, CreatedBy.Email
                       FROM AsyncApexJob
                       WHERE id = :BC.getJobId()];
            String body = 'Your batch job '
             + 'BatchApexAccountUpdate '
             + 'has finished. \n' 
             + 'There were '
             + errorMap.size()
             + ' errors. Please find the error list attached to the Case.';
            // Creating the CSV file
            String finalstr = 'Id, Name, Error \n';
            String subject = 'Account - Apex Batch Error List';
            String attName = 'Account Errors.csv';
            for(Id id  : errorMap.keySet()){
                string err = errorMap.get(id);
                Account acct = (Account) IdToSObjectMap.get(id);
                string recordString = '"'+id+'","'+acct.Name+'","'+err+'"\n';
                finalstr = finalstr +recordString;
            // Define the email
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 
            // Create the email attachment    
            Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
            // Sets the paramaters of the email
            email.setSubject( subject );
            email.setToAddresses( new String[] {a.CreatedBy.Email} );
            email.setPlainTextBody( body );
            email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
            // Sends the email
            Messaging.SendEmailResult [] r = 
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});   
Anilkumar KotaAnilkumar Kota
Hi You are not updating the list here like Update accountlist. once you can do this and check.