This question is very similar to this issue, however I have a slight derivation.
Currently I am upserting Cases via the Data Loader, this way I can insert Cases and relate some look-up fields by their External Id.
The problem I'd like to solve is finding out if it is possible to do the following:
If a reference is found, proceed as normal. If the External Id doesn't match a value, save some other information.
Basically, is there a way to "DMLException when lookup's external id is unknown" from within a trigger?
Here is an example:
I upsert some Cases via data loader, using an External Id to populate some look-up fields. In the trigger, I handle the failures somehow. Here's what I am guessing:
trigger CaseTrigger on Case (before insert, after insert)
{
if(Trigger.isInsert && Trigger.isBefore)
{
//If the Case used an External Id and has a success looking up its related record
//Do some operation
}
//Not sure if the Exception gets thrown here
(Trigger.isInsert && Trigger.isAfter)
{
//If the Case failed due to DMLException when lookup's external id is unknown
List<Case> casesToFix = CaseFilter.FindCasesToFix(Trigger.New);
CaseHandler.FixCasesWithInvalidExernalId(fileredCases);
}
}
I could simply ask the Client to work over the Cases twice. Once to Upsert records for the reference, then Insert any records that failed with a different Mapping. However, I was curious if I could avoid that step by doing some record clean-up within the trigger.