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
Mayank Upadhyaya 5Mayank Upadhyaya 5 

Batch apex to Update opportunity child record from Parent child record

Hi everyone i  am new in apex . I have a requirement Batch where i need to Update all opportunity child record from Parent child record from batch apex . Parent opportunity and child opportunity have looke up relationship and look up field is composite_opportunity__c
i have condition where child records should be updated with the latest field values of Parent opportunity and batch apex should be run daily. These are below fields which need to update on child opportunity 

Proposed_Net_PMPM_Revised__c, Proposed_GPMGIPM__c, Certified_Equipment__c, Total_Machines_Pockets__c, IncentivePayments__c, Betterments__c, VentingPlumbing__c, Wiring__c, Other_Capital__c, Capital_Total_Revised__c, Prev_Net_PMPM__c, IRR_2014__c, Lease_Term_Months__c, Monthly_Cashflow_VCI__c, Proposed_Gross_PMPM_Include_Inc__c, Average_Vend_Price__c, Midterm_Vend_Increase_Percentage__c, Prop_Gross_Monthly_Revenue__c, Scoring_Model_Total_Points__c, Proposed_Net_PMPM_VCI__c, Equipment_Total__c, Status__c, Capital_Coverage__c, Payback_Percentage__c, NRPM_Percentage__c, Commission_Percentage__c, Certified_Equipment_Percentage__c, Non_Equipment_Percentage__c, VCI__c, NPV_Cashflow_VCI__c, Months_Remaining_Existing_Contract__c, Override_Months_Remaining_New__c, Existing_Equipment_Count__c

Please let me know if anyone have sample code for this logic.

Thanks
PriyaPriya (Salesforce Developers) 
Hi Mayank,

Please try this Pseudo code and modify it accordingly :- 
 
global class BatchUpdateAllRelatedInv implements Database.Batchable<sObject> {
    public Opportunity opp{ get; set; }
    String query;

    global BatchUpdateAllRelatedInv() {

    }

    global Database.QueryLocator start(Database.BatchableContext BC) {
        this.query = 'Select Id, OppAmount__c from  RelatedInventory__c';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List< RelatedInventory__c> scope) {
        this.opp = [Select Id, Name, Amount, StageName, Probability from Opportunity
                            where Id =: ----"how I will get the Id of that specific Opp"];
        for( RelatedInventory__c rINV :scope){
            rINV.OppAmount__c = opp.Amount;
        }
        update scope;
    }

    global void finish(Database.BatchableContext BC) {

    }

}


for more detail please refer below link :-

https://salesforce.stackexchange.com/questions/83408/how-to-update-all-child-record-in-the-system-using-parent-field-value-using-batc

 

regards,

Priya Ranjan