Assigning a Value to a Field in a Related Object
To assign a value to a field in a related object, use the assignment operator like this:
// Assume script runs in context of an Activity_c object (child of TroubleTicket_c)
// and that TroubleTicket is the name of the parent accessor
TroubleTicket.Status_c = 'Open'
Since a child object must be owned by some parent row, you can assume that the
TroubleTicket
accessor will always return a valid parent row
instead of ever returning null. This is a direct consequence of the fact that the parent
foreign key value in the Activity
object’s
TroubleTicket_Id
field is mandatory. In this situation, it is not
strictly necessary to use the Groovy safe-navigation operator, but in
practice is it always best to use it:
TroubleTicket?.Status_c = 'Open'
By following this advice, you can be certain your code will never fail with a
NullPointerException
error when you happen to work with an accessor
to a related object that is optional. For example, suppose you added a custom dynamic
choice field named SecondaryAssignee_c
to the Activity
object and that its value is optional. This means that referencing the related accessor
field named SecondaryAssignee_Obj_c
can return null if the related
foreign key field SecondaryAssignee_Id_c
is null. In this case, it is
imperative that you use the safe-navigation operator so that the attempted assignment is
ignored when there is no secondary assignee for the current activity.
SecondaryAssignee_Obj_c?.OpenCases_c = caseTotal
For more information on accessing related objects see Using the Related Object Accessor Field to Work with a Parent Object and Using the Related Object Accessor Field to Work with a Referenced Object..