[SalesForce] How Does Upsert Work When No External Id Is Specified

I have looked at the documentation on Database Methods. What I do not understand is, what happens when an upsert is performed without specifying an external id field? Is it just based on whether or not the record has an Id?

Best Answer

It just simply check if ID field is set then SFDC update the record orther wise it just insert record.

for ex:

List<Account> al = new list<Account>();

Account a1 = new account(ID='0019000001dK6Rd', Name='XXXX Test Account 1'); 
al.add(a1); // This is updated as ID is passed 

Account a2 = new account(MyExtID__c='Target#001', Name='XXXX Test Account 2'); 
al.add(a2);// This is updated as External ID is passed

Account a3 = new account( Name='XXXX Test Account 3'); 
al.add(a3);// This is inserted as both ID & External ID is NOT passed

Database.upsert(al); 

Update:

Usage:
Upsert is a merging of the words insert and update. This call is available for objects if the object has an external ID field or a field with the idLookup field property.

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_calls_upsert.htm

Related Topic