Relating errors in a before insert trigger to the originating record

before-triggertrigger

I have a before insert trigger on leads that translates a few fields that are populated from a web form that is translated into multiple languages. This is working well.

If there is no matching translation found, I want to flag the record as having an error so it can be resolved. I would like to log an activity for this, to avoid cluttering up the object with fields for specific integrations. However, it seems like this is not possible because I cannot access the Id in a before insert trigger, so I cannot related the activity to the lead.

Is the only option to use a field on the lead to flag issues? Is there some trick to this that I am missing?

This must be a before insert trigger because some of the fields are restricted picklists, and the translations must process before the insert. The trigger sets the picklist to null if there is no matching translation to allow the lead to be inserted.

Best Answer

If it must be on a before trigger, then you're out of luck. On insert, the record won't have an Id until the after trigger.

Why don't you still leave the processing of the translations to the before trigger, but also add some checking in the after trigger to see if there's a translation, and if there's not, create the activity?

Related Topic