• #11165
    Salesforce | Himanshu Himanshu #381
    Forcetalks

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

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

    Tagged: , , , , , , , ,

    #11197
    Salesforce | Piyush Piyush #16
    #24794
    Salesforce | PRANAV PRANAV #1
    Forcetalks

    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.

    #27069
    Salesforce | Adarsh Adarsh #4
    Forcetalks

    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;
    }
    }

    #27157
    Salesforce | Archit Archit #2
    Forcetalks

    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;
    }
    }
Viewing 5 posts - 1 through 5 (of 5 total)

Please to reply to this topic.

CONTACT US

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

Sending

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 2018 Forcetalks. All Right Reserved.

Log in with your credentials

or    

Forgot your details?