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
lakshmi c 1lakshmi c 1 

write a batch class to update all the phone numbers on contact object with the corresponding account phone number?

Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi lakshmi,
Try this code
global class contactPhoneUpdate implements Database.Batchable<sObject>
{
	    List <Contact> toUpdate = new List <Contact> ();

	global Database.QueryLocator start(Database.BatchableContext BC)
	{
        String query = 'SELECT Id,phone,account.phone from contact';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<contact> scope)
    {
        for ( contact c : scope)
        {
           c.Phone=c.account.phone;
           toUpdate.add(c); 
        }
        update toUpdate;
    }  
    global void finish(Database.BatchableContext BC)
    {
    }
}

Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards
Suraj Tripathi 47Suraj Tripathi 47
Hii Lakshmi,
please try below batch class code : 

public class contactPhoneUpdateByAccPhone implements Database.Batchable<sObject> {
    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator('SELECT id,Phone,account.Phone FROM Contact');
    }
    public void execute(Database.BatchableContext bc, List<Contact> contactList){
     for(Contact eachContact : contactList)
        {
           eachContact.Phone = eachContact.account.phone;
        }
        update contactList;
    }
    public void finish(Database.BatchableContext bc){
        System.debug('Successfully Update');
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Suraj