+ Start a Discussion
Mike ArthurMike 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.
Yogesh KulkarniYogesh Kulkarni
Hi 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
Mike ArthurMike Arthur
Thanks for your reply Yogesh. I maybe wasn't clear enough in my description - it's not a bunch of detail records to a master, it's custom object records being inserted, each with a lookup to a (potentially different) Quote. I need the value of a field on the Quote for each inserted record in order to populate a field on the custom object record. How does that change things? Thanks, Mike.