Activity Forums Salesforce® Discussions How can we update child records from parent using Salesforce Apex Class?

  • Piyush

    Member
    April 30, 2016 at 4:47 pm
  • PRANAV

    Member
    January 31, 2018 at 11:19 am

    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.

  • Adarsh

    Member
    March 30, 2018 at 10:04 am

    Hi,

    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;
    updates.add(detail);
    }
    }
    update updates;
    }
    }

  • Archit

    Member
    March 30, 2018 at 11:24 am

    Hello,

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

    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;
    updates.add(detail);
    }
    }
    update updates;
    }
    }

Log In to reply.

Popular Salesforce Blogs