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
Phuc Nguyen 18Phuc Nguyen 18 

Update child fields from parent

In an apex class I am trying to update a couple field on a child record from the parent when the child record is created.
Any suggestion is appreciated.
Thanks
Best Answer chosen by Phuc Nguyen 18
David Zhu 🔥David Zhu 🔥
you may use the code below as reference. This is just a mockup and you may need to move the code into your existing trigger handler class.

trigger ProjectProblem on Project_Problem__c (before insert)
{
         List<Id> projectIds = new List<Id>();
         for (Project_Problem projectProblem : trigger.New)
         {
            projectIds.add(projectProblem.project__c);     //assume master-detail relationship field is called project__c
          }

         Map<Id,Project__c>  projectMap = new Map<Id,Project__c>([select id, field1__c,field2__c,..... from Project__c where id in : projectIds]);

         for (Project_Problem projectProblem : trigger.New)
         {
                 projectProblem.fieldTobeUpdated1__c = projectMap.get(projectProblem.project__c).field1__c;
                 projectProblem.fieldTobeUpdated2__c = projectMap.get(projectProblem.project__c).field2__c;
         }
}

All Answers

David Zhu 🔥David Zhu 🔥
I think what you want is  when the child record is created, you want to pull/check cerntain infomration on parent record, then update the child record field.
In that case, I would suggest using process builder or trigger. if the logic is simple, I would prefer process builder
Phuc Nguyen 18Phuc Nguyen 18
Thanks for the reply David.  I thought about the process builder but I already had a trigger that calls a class and I will have numerous updates which may make the PB harder to read.  I just need help getting started with one filed and I will replicate the rest.  I have a field called 'lease' on the project(parent) and I if its populated I need to copy to the newly created Project_Problem record. 
Thanks,
P
David Zhu 🔥David Zhu 🔥
you may use the code below as reference. This is just a mockup and you may need to move the code into your existing trigger handler class.

trigger ProjectProblem on Project_Problem__c (before insert)
{
         List<Id> projectIds = new List<Id>();
         for (Project_Problem projectProblem : trigger.New)
         {
            projectIds.add(projectProblem.project__c);     //assume master-detail relationship field is called project__c
          }

         Map<Id,Project__c>  projectMap = new Map<Id,Project__c>([select id, field1__c,field2__c,..... from Project__c where id in : projectIds]);

         for (Project_Problem projectProblem : trigger.New)
         {
                 projectProblem.fieldTobeUpdated1__c = projectMap.get(projectProblem.project__c).field1__c;
                 projectProblem.fieldTobeUpdated2__c = projectMap.get(projectProblem.project__c).field2__c;
         }
}
This was selected as the best answer
Phuc Nguyen 18Phuc Nguyen 18
Thank you David.  Does it make a difference if the Project objetc is a Lookup and not Master detail?
Thanks
P
 
David Zhu 🔥David Zhu 🔥
They are the same from above code perspective.
Phuc Nguyen 18Phuc Nguyen 18
Hey David, 
Can this be made as any after insert? 
There is validation in the code which is causing the save to fail and if its in the after insert it states the record is read only.
P
Phuc Nguyen 18Phuc Nguyen 18

So the before insert works if I take the actual field update out:

projectProblem.fieldTobeUpdated1__c = projectMap.get(projectProblem.project__c).field1__c;
Not sure what is going on here.

David Zhu 🔥David Zhu 🔥
You can put in after insert but it needs some changes.

trigger ProjectProblem on Project_Problem__c (after insert)
{
         List<Project_Problem__c> projectProblems = [select id,project__c,fieldtobeUpdate1__c, fieldtobeupdated2__c .... from Project_Problem__c where Id in :trigger.newMap.keyset()];

         List<Id> projectIds = new List<Id>();
         for (Project_Problem projectProblem : projectProblems )
         {
            projectIds.add(projectProblem.project__c);     //assume master-detail relationship field is called project__c
          }

         Map<Id,Project__c>  projectMap = new Map<Id,Project__c>([select id, field1__c,field2__c,..... from Project__c where id in : projectIds]);

         for (Project_Problem projectProblem : projectProblems)
         {
                 projectProblem.fieldTobeUpdated1__c = projectMap.get(projectProblem.project__c).field1__c;
                 projectProblem.fieldTobeUpdated2__c = projectMap.get(projectProblem.project__c).field2__c;
         }

         update projectProblems;
}