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;
