+ Start a Discussion
CBBsfdcCBBsfdc 

Write a Batch Apex to update all the Account records as below:

UK in Billing Country or Shipping Country should get updated to United Kingdom.
US or USA in Billing Country or Shipping Country should get updated to United States
Best Answer chosen by CBBsfdc
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
global class Batch_UpdateAcc implements Database.Batchable<sObject>{
    
    List <Account> mapAccount = new List <Account> ();
    
    global Database.QueryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator('SELECT ID, BillingCountry, ShippingCountry FROM Account');
    }
    
    global void execute(Database.BatchableContext bc, List<Account> scope){
        for(Account acc : scope){
            if(acc.BillingCountry=='UK'){
                acc.BillingCountry = 'United Kingdom';
            }
            if(acc.ShippingCountry=='UK'){
                acc.ShippingCountry = 'United Kingdom';
            }
            if(acc.BillingCountry=='USA'){
                acc.BillingCountry = 'United States';
            }
             if(acc.ShippingCountry=='USA'){
                acc.ShippingCountry = 'United States';
            }
            mapAccount.add(acc);
        }
        UPDATE mapAccount;        
    }
    
    global void finish(Database.BatchableContext bc){
        
    }     
}

Execute in Anonymous Window:
 
Id batchJobId = Database.executeBatch(new Batch_UpdateAcc(), 200);

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas