Upsert | APEX DML Standalone Statements
Chapter Topics
- What Is DML In Salesforce?
- Insert | APEX DML Standalone Statements
- Update | APEX DML Standalone Statements
- Upsert | APEX DML Standalone Statements
- Delete | Apex DML Standalone Statement
- Undelete | Apex DML Standalone Statement
- Merge | APEX DML Standalone Statements
- APEX DML Statements Best Practices
- Database Class in APEX
- Empty Recycle Bin
- Count Query | Method in Apex Database Class
- Lead Conversion | Method in Apex Database Class
- Transaction Control and Rollback
- Database Class Method Result Object
Upsert In Salesforce
To create or update the existing data, you need to use an upsert statement.
Example
List < Contact > newConList = new List < Contact > ();
List < Contact > conList = [SELECT firstname, lastname FROM contact];
Integer i = 1;
for (Contact cc: conList) {
String s = cc.name;
if (s.conatins(‘Contact’)) {
cc.phone = ‘1234’;
} else {
Contact c1 = new Contact(lastname = ’Contact’ + i);
i++;
}
newConList.add(cc);
}
upsert newconList;

Join our newsletter: Get daily update on Salesforce career insights & news!
Join Now!
Need more support?
Get a head start with our FREE study notes!
Learn more and get all the answers you need at zero cost. Improve your skills using our detailed notes prepared by industry experts to help you excel.