0

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.

1 Answer 1

0

You should be able to accomplish it through the use of the Database Class. There is an option you can have for "all or none". This means that it will default to either all being successful or none will get inserted. Code defaults to True on that particular value (the data loader has that setting to false by default). So if you do something like this:

Database.Upsert(myList,myExternalIdReference,false);

That should resolve your issue. I think the biggest pain will be defining the external ID's, but it sounds like you have that resolved. More information on how to do that can be found in the link.

Also, you'll be able to obtain the results and work with them in some other fashion as well if you do this:

List<Database.UpsertResult> upResults = Database.Upsert(myList,myExternalIdReference,false);

More information on upsert result is here.

4
  • That's not exactly what I am looking for. If I was able to use the Database.Upsert command, I'd be in the clear and just work through the List<Database.UpsertResult> object. However, when records are inserted into Salesforce via the Data Loader, I have less control there. I am basically trying to avoid Upserting records through the Data Loader twice. Commented Jul 31, 2015 at 18:35
  • 1
    @ProgrammableMedley Ah you said trigger, so I thought you were doing it pragmatically. Would you be able to load up a custom field with the foreign key values in non lookup fields, then try to do the lookups via a trigger (similar to what an upsert would do, but just before insert)? That seems like an elegant way to handle it, that way you still have the records inserted, but not necessarily attached? Commented Jul 31, 2015 at 18:42
  • Yeah, I was trying to avoid that as well, but it seems like I may have to do it. :: shrugs :: If I don't see a better solution in 15 minutes or so, that's the approach I move forward with. Commented Jul 31, 2015 at 18:45
  • Yeah, the problem is that by the time that it gets to the trigger, it's already either failed or succeed if I recall correctly. Commented Jul 31, 2015 at 19:05

You must log in to answer this question.

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