+ Start a Discussion
ramesh babu 114ramesh babu 114 

What is upsert? How it works?

What is upsert? How it works?
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Ramesh,
Using the upsert operation, you can either insert or update an existing record in one call. To determine whether a record already exists, the upsert statement or Database method uses the record’s ID as the key to match records, a custom external ID field, or a standard field with the idLookup attribute set to true. hope it helps.

Please mark it as best answer if the information is informative.so that question is removed from an unanswered question and appear as a proper solution.

Thanks
Rahul Kumar
Gopi MarriGopi Marri
Upsert: The upsert DML operation creates new records and updates sObject records within a single statement, using a specified field to determine the presence of existing objects, or the ID field if no field is specified.

Upserting Records:
-----------------------
If you have a list containing a mix of new and existing records, you can process insertions and updates to all records in the list by using the upsert statement.
 Upsert helps avoid the creation of duplicate records and can save you time as you don’t have to determine which records exist first.
 
Upsert uses the sObject record's primary key (the ID), an idLookup field, or an external ID field to determine
whether it should create a new record or update an existing one:
If the key is not matched, a new object record is created.
If the key is matched once, the existing object record is updated.
If the key is matched multiple times, an error is generated and the object record is neither inserted or updated.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_examples_upsert.htm
Vid ClarkeVid Clarke

Q: If I have a comment field and some records have information populated in them and some don't, would using Upsert allow me to add additional information to comment field with data already in them or will it overwrite them? 

I'm looking at updating several thousand records and need to add additional information for a period of time and then remove that info at a later date.

Yash Shrivastava 3Yash Shrivastava 3
Hi, vid
in my knowledge, data in your comment field get updated when you use upsert operation because it matched the salesforce id or either external id and if there is no data in your comment field it will get inserted same time.

Thanks
Yash Shrivastava