1

Let's assume I have a list of 100 new records that I want to insert, 10 of these records are missing information which will result in DML exception.

Is there a way for me to create a list and include all the problematic records in it?

3
  • Are you running an anonymous apex script, inserting using an apex class, or using a tool such as dataloader?
    – bpilling
    Commented Feb 26, 2020 at 10:10
  • I want to build a class that sends out emails to the developers if there is an issue/error.
    – Json
    Commented Feb 26, 2020 at 10:21
  • this can help probably salesforce.stackexchange.com/questions/42417/…
    – User6670
    Commented Feb 26, 2020 at 10:41

1 Answer 1

3

You can use the SaveResult Class methods.

If you insert the records using Database.insert / Database.update with a false second parameter (allOrNothing) to allow partial processing of records on failure you will be able to retrieve an array of SaveResult objects. Each of those SaveResult records has a isSuccess() method which returns false in case the DML operation was not successful. From there you are able to retrieve the specific error per record that failed to be inserted and/or updated.

If you want to do the same for a Database.delete method instead then you need to use the DeleteResult class.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .