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
kezia dolakezia dola 

I want to update a field in child object when parent object record is updated

Hi All,

I have a field called order__c in quote and quote line item objects. Using trigger, I want to update order__c field in quote line item, when order__c field in quote is updated. 

Any Idea how to do this?

Thank you,
Kezia
Best Answer chosen by kezia dola
ravi soniravi soni
hi Kezia,
use below trigger and let me know what is your status after using it.
trigger UpdateQuoteLineItem on Quote (after update) {
    if(trigger.isAfter && trigger.isUpdate){
        Set<string> quoteIdSet = new Set<string>();
        list<QuoteLineItem> lstQuoteLineItem = new list<QuoteLineItem>();
        for(Quote oQuote : trigger.new){
            if(oQuote.Order__c != null){
                if(oQuote.Order__c != trigger.oldMap.get(oQuote.Id).Order__c){
                    quoteIdSet.add(oQuote.Id);
                }
                
            }
        }
        
        for(Quote existQuote : [SELECT Id,Name,Order__c,(SELECT Id,Order__c FROM QuoteLineItems) FROM  Quote
                                 WHERE Id In : quoteIdSet]){
                                     for(QuoteLineItem childQli : existQuote.QuoteLineItems){
                                         childQli.Order__c = existQuote.Order__c;
                                         lstQuoteLineItem.add(childQli);
                                     }
                                     
                                 }
        
        if(lstQuoteLineItem.size() > 0){
            update lstQuoteLineItem;
        }
        
    }
    
}

don't forget to mark it as best answer.
Thank you

All Answers

AbhinavAbhinav (Salesforce Developers) 
Hi Kezia,

You can take reference from below bog to update child records when parent record is updated:

https://www.infallibletechie.com/2014/08/how-to-update-child-records-when-parent.html

Thanks!
ravi soniravi soni
hi Kezia,
use below trigger and let me know what is your status after using it.
trigger UpdateQuoteLineItem on Quote (after update) {
    if(trigger.isAfter && trigger.isUpdate){
        Set<string> quoteIdSet = new Set<string>();
        list<QuoteLineItem> lstQuoteLineItem = new list<QuoteLineItem>();
        for(Quote oQuote : trigger.new){
            if(oQuote.Order__c != null){
                if(oQuote.Order__c != trigger.oldMap.get(oQuote.Id).Order__c){
                    quoteIdSet.add(oQuote.Id);
                }
                
            }
        }
        
        for(Quote existQuote : [SELECT Id,Name,Order__c,(SELECT Id,Order__c FROM QuoteLineItems) FROM  Quote
                                 WHERE Id In : quoteIdSet]){
                                     for(QuoteLineItem childQli : existQuote.QuoteLineItems){
                                         childQli.Order__c = existQuote.Order__c;
                                         lstQuoteLineItem.add(childQli);
                                     }
                                     
                                 }
        
        if(lstQuoteLineItem.size() > 0){
            update lstQuoteLineItem;
        }
        
    }
    
}

don't forget to mark it as best answer.
Thank you
This was selected as the best answer
kezia dolakezia dola

Hi veer soni,

Thanks, I tried with the trigger and it is updating now. 

Thank you,
Kezia