All Contact Names Update using Batch Apex in salesforce

By | December 23, 2019

Hi guys, here we are going to explain how to update all contact last name records with lastname +’salesforcecodes’.

Apex Class

global class batchContactUpdate implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT Id,lastName FROM contact';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<contact> con) {
        for(contact c : con){
            c.lastName = c.lastName + 'salesforcecodes';
            // to remove updated value from all records then
            // we need to write 9th line with c.lastName = c.lastName.remove('salesforcecodes');           
        }
        update con;
    }   
    
    global void finish(Database.BatchableContext BC) {
    }
}

To check the output, call the apex class in debug Anonymous window.

on the top ==> debug || debug Anonymous window

call the class as shown in below image.

Leave a Reply

Your email address will not be published. Required fields are marked *