• #11165
    Salesforce | Himanshu Himanshu #192

    How can we update child records from parent using Salesforce Apex Class?

    How can we update child records from the parent using apex class?


    Salesforce | Piyush Piyush #174
    Salesforce | PRANAV PRANAV #954

    Hi Himanshu,

    Try the below code, or you can take an idea from this.

    try {
    // Query for the contact, which has been associated with an account.
    Contact queriedContact = [SELECT Account.Name
    FROM Contact
    WHERE FirstName = ‘Joe’ AND LastName=’Smith’
    LIMIT 1];

    // Update the contact’s phone number
    queriedContact.Phone = ‘415.555.1213’;

    // Update the related account industry
    queriedContact.Account.Industry = ‘Technology’;

    // Make two separate calls
    // 1. This call is to update the contact’s phone.
    update queriedContact;
    // 2. This call is to update the related account’s Industry field.
    update queriedContact.Account;
    } catch(Exception e) {
    System.debug(‘An unexpected error has occurred: ‘ + e.getMessage());

    Hope this helps you.

    Salesforce | Adarsh Adarsh #958


    you may use the following code,

    trigger IPAapproved on Outbound_Sales_Order__c (after update) {

    // Only do work when the field has changed
    Map<Id, Outbound_Sales_Order__c> changed = new Map<Id, Outbound_Sales_Order__c>();
    for (Outbound_Sales_Order__c oso : Trigger.new) {
    Outbound_Sales_Order__c old = Trigger.oldMap.get(oso.Id);
    if (oso.Buyer_Approved__c != old.Buyer_Approved__c) {
    changed.put(oso.Id, oso);
    if (changed.size() > 0) {
    // Only update when the field has the wrong value
    List<SO_Detail__c> updates = new List<SO_Detail__c>();
    for (SO_Detail__c detail : [
    SELECT Id, Outbound_Sales_Order__c, Buyer_Approved_Flag__c
    FROM SO_Detail__c
    WHERE Outbound_Sales_Order__c IN :changed.keySet()
    ]) {
    Outbound_Sales_Order__c oso = changed.get(detail.Outbound_Sales_Order__c);
    Boolean requiredFlag = oso.Buyer_Approved__c != null;
    if (detail.Buyer_Approved_Flag__c != requiredFlag) {
    detail.Buyer_Approved_Flag__c = requiredFlag;
    update updates;

    Salesforce | Archit Archit #957


    Call the below trigger in apex class you can achieve the functionality what you want

Viewing 5 posts - 1 through 5 (of 5 total)

Please to reply to this topic.

Popular Salesforce Blogs


We're not around right now. But you can send us an email and we'll get back to you, asap.


About Us

Forcetalks is a Salesforce collaboration platform for coders & developers, geeks & nerds, consultants & business heads, admins & architects, managers & marketers and of course the business owners. A community where you can learn from, where you can contribute to. For you. For Salesforce. Read More...

Copyright 2020 Forcetalks. All Right Reserved.

Log in with your credentials


Forgot your details?