Skip to content

Instantly share code, notes, and snippets.

@sudikrt
Created August 14, 2017 08:22
Show Gist options
  • Save sudikrt/c38743878898d5908d95f3ecbc7bec6d to your computer and use it in GitHub Desktop.
Save sudikrt/c38743878898d5908d95f3ecbc7bec6d to your computer and use it in GitHub Desktop.
Salesforce DML and its methods
Contact [] delContact = [SELECT Id FROM Contact where LastName = 'YLast'];
delete delContact;
System.debug(delContact);
try {
Account account = new Account ();
insert account;
} catch (DmlException e) {
System.debug('Error occured' + e.getMessage ());
}
//List <Contact> dummyList = ;
Database.SaveResult [] allRes = Database.insert(new List<Contact> {
new Contact (FirstName = 'Nony_0', LastName = 'Mang'),
new Contact (FirstName = 'Nony_1', LastName = 'Mang'),
new Contact (FirstName = 'Nony_2', LastName = 'Mang'),
new Contact (FirstName = 'Nony_3', LastName = 'Mang'),
new Contact ()
}, false);
for (Database.SaveResult res : allRes) {
if (res.isSuccess ()) {
System.debug('inserted successfully : ' + res);
} else {
for (Database.Error err : res.getErrors ()) {
System.debug('Error in inserting data : ' + err);
}
}
}
Database.DeleteResult [] resDel = Database.delete([SELECT Id from CONTACT where FirstName LIKE 'Nony_%']);
for (Database.DeleteResult resD : resDel) {
System.debug(resD);
}
List <Contact> ctList = new List<Contact> { new Contact (FirstName = 'Raki_jab',
LastName = 'KG',
Email = '[email protected]',
Department = 'Marketing',
Description = 'New Lead person'),
new Contact (FirstName = 'Nandi',
LastName = 'Jack',
Email = '[email protected]',
Department = 'Technology',
Description = 'New Lead person'),
new Contact (FirstName = 'Mahi',
LastName = 'KG',
Email = '[email protected]',
Department = 'Finance',
Description = 'New Lead person'),
new Contact (FirstName = 'Jan',
LastName = 'KG',
Email = '[email protected]',
Department = 'Finance',
Description = 'New Lead person')
};
//System.debug(ct);
insert ctList;
System.debug(ctList);
List<Contact> listToUpdate = new List<Contact>();
for (Contact ct : ctList) {
if (ct.Department == 'Finance') {
ct.Title = 'Finance Analyst';
listToUpdate.add(ct);
}
if (ct.Department == 'Marketing') {
ct.Title = 'Marketing Analyst';
listToUpdate.add (ct);
}
}
update listToUpdate;
System.debug(listToUpdate);
Contact contact_j = new Contact (FirstName = 'Josh_B', LastName = 'Kaplan', Email = '[email protected]');
insert contact_j;
System.debug (contact_j);
contact_j.Description = 'This is the joshs contact';
Contact contact_K = new Contact (FirstName = 'Ketti', LastName = 'Mairo', Email = '[email protected]');
List <Contact> upsertList = new List<Contact> {contact_j, contact_k};
upsert upsertList;
System.debug (upsertList);
/*
Upsert with email external look up field
*/
Contact personX = new Contact (
FirstName = 'PersonX',
LastName = 'XLast',
Email = '[email protected]',
Description = 'Description for the personX'
);
insert personX;
System.debug (personX);
Contact personY = new Contact (FirstName = 'PersonX',
LastName = 'YLast',
Email = '[email protected]',
Description = 'This is the updated description');
upsert personY Contact.fields.Email;
System.debug(personX);
System.debug(personY);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment