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
Rajesh Singh 88Rajesh Singh 88 

Update existing child records (Object A) from child records (Object B)

OrderItem is the parent to Object A and Object B.

I want to update Type__c  on Object A (existing records) from Object B when the  Object B records get inserted with the Type__c.

I think I can leverage the OrderItem lookup on both the child objects to have a condition something like 
Trigger :- afterUpdate on Object B

If(objectA.OrderItem == objectB.OrderItem){
objectA.Type__c = objectB.Type__c
list.add(objectA); 
}
update list;

As I am bit new to apex not sure how to go ahead. Need some help on this.
Best Answer chosen by Rajesh Singh 88
Suraj Tripathi 47Suraj Tripathi 47
Hi,

You can take reference from  the below code:-
// I have taken below object accoding to your requrements.
//  Object b = Opportuntiy
//  Object A = Contact
// Order = account

trigger OpportunityTrigger on Opportunity (after update,after insert) {

if(trigger.isafter)
    {   
        if(trigger.isinsert || trigger.isUpdate){
 
        ContactVSOppType.disp(trigger.new);
     }
       
    }  
////////////////////////////////////////////////

public class ContactVSOppType {
    public static void disp(List<opportunity> oppList){
        set<id> s1 = new  set<id> ();
        for(Opportunity o: oppList){
            s1.add(o.accountid);
        }
        List<contact> conList = [select id,Type__c,Accountid from contact where Accountid in: s1];
          for(Opportunity o: oppList){
              for(contact c: conList){
                  if(o.AccountId == c.accountid){
                      c.Type__c = o.Type;
                  }
        }
        }
        update conList;
    }
}


Please mark it as Best Answer if it helps you.

Thanks & Regards
Suraj Tripathi

All Answers

Suraj Tripathi 47Suraj Tripathi 47
Hi,

You can take reference from  the below code:-
// I have taken below object accoding to your requrements.
//  Object b = Opportuntiy
//  Object A = Contact
// Order = account

trigger OpportunityTrigger on Opportunity (after update,after insert) {

if(trigger.isafter)
    {   
        if(trigger.isinsert || trigger.isUpdate){
 
        ContactVSOppType.disp(trigger.new);
     }
       
    }  
////////////////////////////////////////////////

public class ContactVSOppType {
    public static void disp(List<opportunity> oppList){
        set<id> s1 = new  set<id> ();
        for(Opportunity o: oppList){
            s1.add(o.accountid);
        }
        List<contact> conList = [select id,Type__c,Accountid from contact where Accountid in: s1];
          for(Opportunity o: oppList){
              for(contact c: conList){
                  if(o.AccountId == c.accountid){
                      c.Type__c = o.Type;
                  }
        }
        }
        update conList;
    }
}


Please mark it as Best Answer if it helps you.

Thanks & Regards
Suraj Tripathi
This was selected as the best answer
Abdul KhatriAbdul Khatri
Hi Rajesh,

As you mentioned OrderItem is Parent to ObjectA and ObjectB, which in Salesforce is termed as MasterDetail Relationship where OrderItem is Master and ObjectA and ObjectB are Details. Keeping this in perspective, it means there will be multiple ObjectA and ObjectB records with one OrderItem. So trying to understand what is the relationship between ObjectA and ObjectB based on the Type. 

You said when ObjectB inserted, you want to update the Type__c to ObjectA Type__c, are you saying you are going to update to all the records in ObjectA for the OrderItem. What about vice versa, if you add ObjectA with a different Type__c then ObjectB?

I think the design need some clarity based on what you are looking and the code goes accordingly.
Rajesh Singh 88Rajesh Singh 88
Hi Adbul,

OrderItem is having lookup relationship with both ObjectA and ObjectB. 

ObjectA is already in the database with the OrderItem lookup field populated so my requirement is when ObjectB will be inserted with the Type__c field populated the data just needs to be mapped on to Type__c field of  ObjectB.

No not on all the records of Object A. It be only when  (objectA.OrderItem == objectB.OrderItem)

Thanks,
Rajesh