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
Daniel KDaniel K 

logic to update object fields

Hi All,
         I have two objects from where I have to compare Id field and update few fields on one of the objects.

         Let's say..
                   select field1 from object1 where condition=true  --> here i have 100k records

                   select field2 from object2 where condition = true  --> here I have 90k records

             Here, if field1=field2 then I have to update object1.field3='abc' ,object1.field4='xyz' and so on

             I think I can do this just by using List and Loop(haven't really tried though), but how to skip the governor limits of updating more than 50k records ?
               Please guide in solving this.
Hi Daniel,

I hope Batch Apex will be able to tackle this issue.
1) If you have large dataset and that needs to be executed once then you can go for a batch apex and execute it once.
2) If  you have large dataset and that is getting created on daily basis then you can go for daily scheduled batch apex.

Please let me know if this helped and mark as a best answer. 

Daniel KDaniel K
Thanks Ramakant, can you please share some examples or refere to some links.
Sample code for batch class:
 *Class Name: UpdateAccountFromPrimaryContact
 *Description : To Back poulate the values of Primary Contact FName and Primary Contact LName
    on Account From Primary Contacts.   
global class UpdateAccountFromPrimaryContact implements Database.Batchable<sObject>{
    List<Contact> PCList = new List<Contact>();
	List<Account> ACList = New List<Account>();
    //Variable to hold recordtypeid's of Contact 
    global final static ID saturnId = RecordTypeIDs__c.getInstance().Saturn_Contact__c;
    global final static ID localId = RecordTypeIDs__c.getInstance().Contact_Local_Advertiser__c;
    global final static ID cssId = RecordTypeIDs__c.getInstance().CSS_Contact_Record_Type__c;
    //Query string to pass use in Querylocator 
    global final String Query ='Select Id,FirstName,LastName,AccountId FROM Contact WHERE Primary_Indicator__c = true AND (RecordTypeId =:saturnId OR RecordTypeId =: localId OR RecordTypeId =: cssId) AND (Account.Primary_Contact_FName__c = null OR Account.Primary_Contact_LName__c = null) Limit 1000000';
    //Set of Primary Contact's AccountId
    global Set<Id> accountIdSet = New Set<Id>();
    //Map of AccountId and Contact FirstName
    global Map<id, String> accountIdAndPrimaryFirstNameMap = New Map<id, String>();
    //Map of AccountId and Contact LastName
    global Map<id, String> accountIdAndPrimaryLastNameMap = New Map<id, String>();
    //List Account's to update 
    global List<Account> accountsToUpdate = New List<Account>(); 
    //List of Temporary Account's
    List<Account> associatedAccounts = New List<Account>();
    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator(query);     

    global void execute(Database.BatchableContext BC, List<sObject> scope){
            //Temporary list
            List<Contact> primaryContactsList =  New List<Contact>();
            primaryContactsList = (List<Contact>) scope;
            //To iterate over List of Contacts in the scope and get required fields
            for(Contact con: primaryContactsList){
                accountIdAndPrimaryFirstNameMap.put(con.AccountId, con.FirstName);
                accountIdAndPrimaryLastNameMap.put(con.AccountId, con.LastName);
            //Query to get List of Associated Accounts of Primary Contacts
                associatedAccounts = [Select id, Primary_Contact_FName__c, Primary_Contact_LName__c from Account where id in : accountIdSet];
            //To iterate over Accounts and assign value from Primary Contact Fields
            for(Account acc : associatedAccounts ){
                acc.Primary_Contact_FName__c = accountIdAndPrimaryFirstNameMap.get(;
                acc.Primary_Contact_LName__c = accountIdAndPrimaryLastNameMap.get(;
            //To Update the list of Accounts
            if(accountsToUpdate.size() > 0){
                update accountsToUpdate;
        }catch(Exception ex) {
            AuditErrorHelper.createAuditLog('UpdateAccountFromPrimaryContact', ex.getStackTraceString() + ': ' + ex.getMessage() , 'Batch Apex Exception', AuditErrorHelper.EXCEPTION_SEVERITY_HIGH, null);                 

    global void finish(Database.BatchableContext BC){