You need to sign in to do that
Don't have an account?
Mike Arthur
Updating a field with a value from parent during insert trigger
I would appreciate some best practice advice here -
I have an insert trigger to populate a field in the record being inserted into a custom object with the value of a field in the parent object.
From other posts I learned that trigger.new cannot see any related fields.
Taking the value from a formula field on my custom object would normally be a choice but maximum number of object references has been reached due to high number of formula fields so I can't create another.
So I need to look up the value with SOQL and store it in a map with the Id of the record being inserted.
Which means that my before insert trigger must now be an after insert in order to know the Id.
If I query into a map and then update the field on the record being inserted I then need to issue an update DML statement, so in order to populate my field I need an insert and an update.
Seems inefficient - is that the only way?
Thanks,
Mike.
I have an insert trigger to populate a field in the record being inserted into a custom object with the value of a field in the parent object.
From other posts I learned that trigger.new cannot see any related fields.
Taking the value from a formula field on my custom object would normally be a choice but maximum number of object references has been reached due to high number of formula fields so I can't create another.
So I need to look up the value with SOQL and store it in a map with the Id of the record being inserted.
Which means that my before insert trigger must now be an after insert in order to know the Id.
If I query into a map and then update the field on the record being inserted I then need to issue an update DML statement, so in order to populate my field I need an insert and an update.
Seems inefficient - is that the only way?
Thanks,
Mike.
If you are inserting record/s then you must be having paretnt record id in that record set. Collect all into one set and query required information from parent. Then just place those fields in the Map records and leave it commit operation at the end of transaction will save these values for you in the child objects.
Thanks,
Yogesh