Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

In database.upsert, how can I find the updated or inserted records based on externalID?

Asked by Audrey Berry Apr 17, 2023 2.5K views 1 answer
Share

About this question

I am upserting list of records based on externalId as follows:

List results = Database.upsert(lstRecords, CustomObject__c.External_Id__c,true);
The results returns as follows:
(Database.UpsertResult[getErrors=();
                        getId=a1k0j000000P6CmAAK;
                        isCreated=true;
                        isSuccess=true;], 
 Database.UpsertResult[getErrors=();
                        getId=a1k0j000000P6CNAA0;
                        isCreated=false;
                        isSuccess=true;])
The first record is inserted and the 2nd record is updated.

Since I am upserting based on externalId, I need to know for which ExternalId the records have been successfully inserted or updated.


UpsertResult Class doesn't expose any methods. It returns SalesforceId and NOT externalId


I need a results structure like this:

(Database.UpsertResult[getErrors=();
                        getExternalId = '1234';
                        getId=a1k0j000000P6CmAAK;
                        isCreated=true;
                        isSuccess=true;], 
 Database.UpsertResult[getErrors=();
                        getExternalId = '1235';
                        getId=a1k0j000000P6CNAA0;
                        isCreated=false;
                        isSuccess=true;])
What would be the best way to achieve this?

Your answer

1 Answer

More Salesforce discussions