Is there a way to identify inside a trigger (eg. before insert) which field is being used as external Id?

The problem I have is that there is an external system upserting some records on an object (let's say... Account).

Depending on the scenario, that system will do the upsert on Account.ExternalId1__c or on Account.ExternalId2__c through the standard api (so they have to provide a externalIDFieldName).

Inside the trigger I can know if its insert or update (so if a record was found or not matching the external Id provided), but I can't find a way to know which is the field used for that matching.

Is it possible?

1 Answer 1


No, you can't identify which field was used, as you can't tell what data was provided as opposed to what data was loaded from the database.

  • I thought so... but is a pitty though... specially when you want to report errors back giving the external Id that caused it (that in the end is how the identify the record...). I haven't even found an "idea" to up-vote :( Feb 27, 2021 at 9:59
  • @SergioAlcocer That description sounds like you might have asked a X-Y Problem question. An external service should be able to identify the error based on the indexed position of the upsert call. You just can't detect this in Apex, but the caller should know which is which.
    – sfdcfox
    Feb 27, 2021 at 10:59
  • They key part being "should". I technically have 2 problems. First is that the middleware can't identify which records have failed because the part that treats the response doesn't know about the request (and I have no control on that part), so the idea was to give back in the error the id. The 2nd problem is that depending on which field is used, I have to send a notification to different people. both problems get solved if I know the field, but I thought it was too complex to explain and decided to simplify ;) Feb 27, 2021 at 12:22
  • @SergioAlcocer Fair enough. I would say that the only reasonable solution for now would be to have another field that you use specifically for the purpose of mapping in the middleware to identify itself. You might also be able to check the request headers to see what client is calling. I know there was an answer here that described how to do that, but I can't seem to find it right now.
    – sfdcfox
    Feb 27, 2021 at 12:52
  • as I doubt that anyone else would post an alternative, I'll mark it as "most helpful"; but as I said... I have no control over what the external system is sending, so I can't make them give me a dummy field with the value of the externalId used... hence the work-around search ;). Thanks anyways ! Mar 10, 2021 at 23:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .