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

Modifying Parent Object Fields From a child's Trigger

I am attempting to make changes to text fields on the parent object in a master-detail relationship between two custom objects. I'm making these changes in a trigger, using the statement:


Child__c.Master__r.Master_Field__c = 'myText';


Upon executing the trigger, I'm getting the following error:


System.DmlException: Update failed. First exception on row 0 with id a03A00000010LifIAE; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, updateValidationStatus: execution of BeforeUpdate

caused by: System.NullPointerException: Attempt to de-reference a null object


This error message occured when attempting to save one of my child objects. The master and the child both already exist in the database, so I'm not sure why I'm getting null when referencing the parent, unless upward traversal through relationships is not allowed (Which I'm not aware of). Any pointers?


You can't set related objects' field values in this way in a trigger.


DML operations only allow fields on one object to be modified in a given operation.


If you want to set a parent field value when a child is updated, depending on your requirements and if it's a master/detail relationship, you might be able to use workfow field updates (no code). 


Otherwise you will need to gather up the parent changes over the children in the child trigger and issue an update on the parent at the end of the trigger processing.