Activity › Forums › Salesforce® Discussions › How can we update child records from parent using Salesforce Apex Class?
Tagged: Child Object, Child Record, Parent Child Relationship, Salesforce Apex, Salesforce Apex Class, Salesforce Apex Code, Salesforce AppExchange, Salesforce Records, Update Child Records
-
How can we update child records from parent using Salesforce Apex Class?
Posted by Himanshu on April 30, 2016 at 4:40 PMHow can we update child records from the parent using apex class?
Archit replied 8 years, 2 months ago 5 Members · 4 Replies -
4 Replies
-
- [adinserter block='9']
-
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.
-
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;
}
} -
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.