Home SALESFORCEAPEX Salesforce Upsert Call

Salesforce Upsert Call


UpsertResult[] ur= connection.upsert(String externalIdFieldName, sObject[] sObjects);
Upsert is creates new records and updates existing records; uses a custom field to determine the presence of existing records. Use upsert() instead of create() to avoid creating unwanted duplicate records You can process records for the one more than object type in an create() or update() call, but all records must have same object type in an upsert() call.
Upsert is merging of the words insert and update. This call is available for the objects if the object has an external ID field or a field with the idLookup field property.
Select update/create:
* If external ID is not matched, then a new record is created.
* If external ID is matched once, then the existing record is updated.
* If external ID is matched multiple times, then an error is reported.
* When batch updating multiple records where the external ID is the same for two or more records in your batch call, those records will be marked as errors in the UpsertResult file. The records will be neither created nor updated.
Sample Code:
lstAccount = [select Id, Name, Team__c, Phone, from Account];
if(lstAccount.size() == 0) {
AccountObj = new Account();
AccountObj.Name = ‘SFDC’;
AccountObj.Team__c = ‘Engineering’;
AccountObj.Phone = ‘04445658985’;
} else {
AccountObj = lstAccount[0];
try {
upsert AccountObj;
catch (DMLException e) {
system.debug(‘DMLException :’ + e);
ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR,’ERROR While Creating/Updating Account’));

You may also like