Handle Database.SaveResult
Database.insert(records, false) turns on partial success: it returns one SaveResult per record instead of throwing.
Your task
Insert one valid and one invalid Account with partial success, then read the SaveResult list.
String tag = 'AS-SAVERESULT-' + UserInfo.getUserId() + '-' +
String.valueOf(Datetime.now().getTime());
List<Account> accounts = new List<Account>{
new Account(Name = tag + '-valid', Description = tag),
new Account(Description = tag + '-missing-name')
};
Integer successCount = 0;
Integer failureCount = 0;
StatusCode firstError;Requirements
Database.insert(accounts, false)should be presentREQUIRED_FIELD_MISSINGshould be absent from your code- Both results should be printed as
Result count: 2 - The valid insert should be printed as
Success count: 1 - The invalid insert should be printed as
Failure count: 1 - The status code should be printed as
First error: REQUIRED_FIELD_MISSING