Sample Partial DML Apex code in Salesforce

Sample Code:

List < Contact > conList = new List < Contact > {
new Contact(FirstName = 'Test0', LastName = 'Test0', Department = 'Finance'),
new Contact(FirstName = 'Test1', LastName = 'Test1', Department = 'Technology'),
new Contact(FirstName = 'Test2', LastName = 'Test2', Department = 'Finance'),
new Contact()
};

Database.SaveResult[] srList = Database.insert(conList, false);

for (Database.SaveResult sr : srList) {
if ( sr.isSuccess() ) {
// Develop code for sucessfully inserted contacts
System.debug('Successfully inserted contact. Contact ID: ' + sr.getId());
} else {
for ( Database.Error err : sr.getErrors() ) {
// Develop code for failed contacts
System.debug(err.getStatusCode() + ' : ' + err.getMessage() + ' : ' + err.getFields());
}
}
}


Cheers!!!

No comments:

Post a Comment