4. Agenda
• Manipulate Records with DML
• insert
• update
• delete
• upsert
• undelete
• merge
• Bulk DML
• Database Methods and Error Handling
• Transaction Control
• Savepoint
• Rollback
5. Some House Rules
• Mute your mic
• Keep adding questions in Zoom Q&A Window
• No question is too small
• Questions will be answered in last 15 mins
6. Manipulate Records with DML
Create and modify records in Salesforce by using the Data Manipulation Language,
abbreviated as DML
7. Insert and Update Statement
The insert DML operation adds one or more sObjects, such as
individual accounts or contacts, to your organization’s data
ID Field Auto-Assigned to New Records after Insert
// Create the account sObject
Account acct = new Account(Name='PathToCode', Phone='(111)111-1111');
insert acct;
Account myAcct = [SELECT Id, Name, BillingCity FROM Account WHERE Id = :acct.Id];
myAcct.BillingCity = 'San Francisco';
update myAcct;
8. Upsert
• Update+insert
• If the key is not matched, a new object record is created
• If the key is matched once, the existing object record is updated
Contact contact1 = new
Contact(FirstName='Amit',LastName='Chaudhary',Department='Finance');
insert contact1;
contact1.Description = 'Test Update';
Contact contact2 = new
Contact(FirstName='Jitender',LastName='Zaa',Department='Technology');
List<Contact> listCont = new List<Contact> { contact1, contact2 };
upsert listCont;
9. Delete and UnDelete
The ALL ROWS keyword queries all rows for both top level and
aggregate relationships, including deleted records and archived
activities
List<Contact> listCont = [SELECT Id FROM Contact WHERE LastName='Chaudhary'];
delete listCont;
List<Contact> listCont = [SELECT Id FROM Contact WHERE LastName='Chaudhary' ALL
ROWS ];
undelete listCont;
10. Merge
Account masterAcct = [SELECT Id, Name FROM Account WHERE Name = 'IBM.' LIMIT 1];
Account mergeAcct = [SELECT Id, Name FROM Account WHERE Name = 'IBM INDIA' LIMIT 1];
merge masterAcct mergeAcct;
12. Database Methods
• Database methods are static methods available in Database class
• Partial insert is supported
• Includes the optional allorNone parameters that defaults true
• Database.insert()
• Database.update()
• Database.upsert()
• Database.delete()
• Database.undelete()
• Database.merge()
13. Database Methods
List<Account> acctList = new List<Account>();
acctList.add(new Account(Name='ABC'));
acctList.add(new Account(Name='XYZ'));
Database.SaveResult[] srList = Database.insert(acctList, false);
for (Database.SaveResult sr : srList) {
if (sr.isSuccess()) {
System.debug('Successfully inserted account. Account ID: ' + sr.getId());
} else {
// Operation failed, so get all errors
for(Database.Error err : sr.getErrors()) {
System.debug('The following error has occurred.');
System.debug(err.getStatusCode() + ': ' + err.getMessage());
System.debug('Account fields that affected this error: ' + err.getFields());
}
}
}
14. Transaction Control
• setSavepoint
• Rollback
Account a = new Account(Name = 'AAA');
insert a;
System.assertEquals(null, [SELECT AccountNumber FROM Account WHERE Id = :a.Id].AccountNumber);
// Create a savepoint while AccountNumber is null
Savepoint sp = Database.setSavepoint();
// Change the account number
a.AccountNumber = '123';
update a;
System.assertEquals('123', [SELECT AccountNumber FROM Account WHERE Id = :a.Id].AccountNumber);
// Rollback to the previous null value
Database.rollback(sp);
System.assertEquals(null, [SELECT AccountNumber FROM Account WHERE Id = :a.Id].AccountNumber);