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()
);
}
}
}
Sample Code for existing records:
List < Contact > listContacts = [
SELECT Id, LastName
FROM Contact
LIMIT 2
];
if ( listContacts.size() > 0 ) {
for (
Contact objContact : listContacts
) {
objContact.LastName = null;
}
Database.SaveResult[] srList = Database.update(
listContacts,
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()
);
}
}
}
}