Update vs Upsert vs Insert - Explain the difference?
I'm trying to understand when to use insert/update/upsert. As I understand, upsert is a combination of insert and update. I tend to use upsert most often as I do not have to check whether an object is a new object or an existing object. I do not see a reason to use update or insert since upsert calls would work for these calls. Is there a reason I should not be using upsert and instead use the other two?
Update vs Upsert vs Insert
INSERT
Always use to create new records
Support generic SObject type insert
Note:
Must NOT have id in the Sobject instance
UPDATE:
Always use to update the record
Support generic Sobject type updates
Note:
Must have ID in the Sobject instance
UPSERT
Always used in case when INSERT and UPSERT both. It is possible on Sobject depending on whether the ID is present in instance or not.
If ID is present it will UPDATE otherwise, INSERT
We can use External Id type to update the Sobject in case the instance doesn't have an ID in it.
Ex: upsert SObjectList External_Id__c
Note:
It does not support generic Sobject type. You will have to Specify the CustomObject.