Print this page

How to copy child record Id into the Master record field?

Knowledge Article Number 000005280

If you have a lookup field on the Master object or any object and want to populate that field with a child object Id or any related object Id, you can use a trigger to do that. Sample code for the trigger is shown below:


Master object : Opportunity
Child object : CustomObj__c

trigger updateOpportunitywithChild on CustomObj__c (after insert,before update)
     List<Opportunity> opps = new List<Opportunity>();
     Opportunity oppty;

     for(CustomObj__c c:
               oppty = new Opportunity(Id=c.Opportunity__c);
               oppty.CustomObjectId__c = c.Id;
      if(opps.size() > 0)
      Update opps;


promote demote