3 - DML
DML statements are
INSERT / UPDATE / DELETE / UNDELETE
TO DEBUG THE DML
Check the Filter in the Execution Logs
INSERT
the Object Class is copied from API Name
// Inserting one single account
Account acc = new Account(Name='CLR Infotech', Phone='9988998899');
acc.Rating = 'Hot';
insert acc;
OR
create a list and do
insert accounts
OR
// Alternate way to insert the accounts
Database.insert(accounts, false); You can use allorNone or false
DML
try {
insert myRecords;
} catch (DmlException e) {
// Handle exception if any record fails
}
Database Methods
Database.SaveResult[] results = Database.insert(myRecords, false);
for (Database.SaveResult result : results) {
if (!result.isSuccess()) {
// Handle individual record failure
}
}
DELETE / UNDELETE
// get all accounts to delete
List<Account> accounts = [SELECT Id FROM Account WHERE Name LIKE 'Nidhi%'];
// delete using dml statement
delete accounts;
OR
// delete using database methods
//Database.delete(accounts);
// get deleted account records
List<Account> deletedAccounts = [SELECT Name, Phone FROM Account WHERE isDeleted=true ALL ROWS];
//undelete records
undelete deletedAccounts;
Comments
Post a Comment