1

In an insert or update trigger, the records are processed in a batch of 200. We have the trigger.new and trigger.old for the new and old values respectively. Now the trigger code is designed to block some of the records by using .addError method. So is there any way to find out the list of the successfully inserted/updated records? Suppose 200 records are being inserted, but few of them were blocked by the trigger due to duplicate values. Is there any way to find out the list of records that were successfully inserted?

Thanks

3
  • Add error method will rollback everything.
    – MIX DML
    Commented Nov 18, 2015 at 15:22
  • @PrabhatKumar Yes but, there will be other successful records
    – Prajith
    Commented Nov 18, 2015 at 15:42
  • 1
    The method or process that is doing the DML should be using the Database.SaveResult - Read up on that class and you will have all you need
    – Eric
    Commented Nov 18, 2015 at 15:53

2 Answers 2

2

As far as I know, you can't find out which records in a trigger have failed from within that same trigger.

If you have some apex code which contains a DML operation, or if your trigger itself contains a DML operation, you can make use of the System.Database methods Database.insert(), Database.update(), Database.delete() and the like.

Here's a link to the System.Database documentation

These methods return a Database.SaveResult[], which you can loop through, and determine whether a record was saved successfully or not.

The code to make use of this looks something like this

// Assuming that you have some list of sObjects, initialized and populated elsewhere in your code, called sObjectsToUpdate.
List<Id> successes = new List<Id>();
for(Database.SaveResult sr : Database.update(sObjectsToUpdate, false)){
    if(sr.isSuccess()){
        successes.add(sr.getId());
    }
}

The second parameter of the database save methods (well, the ones where a List is the first parameter) is a Boolean called allOrNone. For detecting successes, it's vital to pass false. Passing false means that all records will be attempted to be saved, and successfully saved records won't get rolled back if one (or more) other records fail to save.

Getting a list of failures, for those interested, is a bit more involved, as Database.SaveResult.getId() returns null for records that failed. This is something I was running into quite recently, and this question has two good answers

The gist of the solution for that question is that the order of the Database.SaveResult list will be the same as the order of the list that was passed into the database save method. Keeping track of the list index allows you to tie the two lists together.

Reproducing the code from the accepted answer on that question

List<Account> accountsList = [select Id from Account];

for(Account a : accountsList) {
    a.Name = 'Test',
}

List<Database.SaveResult> res = Database.update(accountsList, false);

//for (Database.SaveResult s : res) { //replaced by the following line
for (Integer i = 0; i < accountsList.size(); i++) {
    Database.SaveResult s = res[i];
    Account origRecord = accountsList[i];
    if (!s.isSuccess()) {
        system.debug(s.getId()); // I get null here
        system.debug(origRecord.Id); //This should be the Id you're looking for
     } 
}
0

I created the following situation using execute anonymous.

  1. Created 4 account records - the first one, did not have a name. The remaining three had a name.
  2. Added the four account records to a list
  3. Inserted the list

Expectation : The first record would throw an exception while the remaining records would be inserted(set partial success = true in database.insert)

I tracked the size of all the context variables of the insert trigger. If you notice, the size of trigger.new was 4 before insert and it has been reduced to 3 in after insert. Hence the trigger.new in after insert contains only those records that were successfully inserted.

Thus trigger.new in the after context can be used to identify the successfully DMLed records in a transaction. Hope this helps.

enter image description here

You must log in to answer this question.

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