I wrote a trigger which, on updating the account program status, updates the Opportunity stage name, which in turns fire another trigger which clones another another opportunity. The trigger which is fired secondly has lot of custom validations. My problem is, if the 25th record in bulk update goes to custom validation, it returns the same error message for all the records since all the code is bulkified. Is there a way to display correct message for corresponding record? Whether the adderror method works only for objects for the which the trigger is fired and doesn't work for cross object update?

  • You can use the Database.insert or Database.update methods for this. They will return a List<SaveResult> that you can then parse to figure out what exactly you want to do. You can also allow everything to succeed except for the failed records using those methods if you desire as well.
    – dphil
    Feb 18, 2015 at 15:11
  • Thanks. But the validations are done in trigger only. I tried using Database.Update method with allorNone as false, but it didn't throw any errors and how could I do it Feb 24, 2015 at 10:14
  • It won't throw errors. U need to parse it yourself.
    – dphil
    Feb 24, 2015 at 14:26

2 Answers 2


You need to make sure you map the errors to the source record that initially caused them. I am making some inferences about your mapping, but it should look something like:

// here accounts needs to be the same reference as your trigger.new records
Map<Id, Account> accountMap = new Map<Id, Account> ( accounts );
    update opportunities;
catch (DmlException dmx)
    for (Integer i = 0; i < dmx.getNumDml(); i++)
        Account source = accountMap.get(opportunities[i].AccountId);
        if (source != null) source.addError(dmx);
  • My validations are present in second trigger, but I am updating on first trigger which in turns fires second trigger. Because of bulk update in first trigger, the bulk operation will fire in second trigger, and if any validations done in it, it returns same validation message for all records to first trigger and in data loader it displays same message for all records. Feb 19, 2015 at 6:41
  • The second trigger can only be fired by an update to Opportunity records somewhere, no? Implement this wherever you perform cross-object updates.
    – Adrian Larson
    Feb 19, 2015 at 16:03
  • Ok thanks. But my validations are made in the same trigger only. Then how could I handle those in exception? I want to give specific error message for each validation. How could I achieve this? Feb 24, 2015 at 10:12
  • You can pass in the DmlException you received to the addError call instead of the generic String I used in my example.
    – Adrian Larson
    Feb 24, 2015 at 15:18

I think , what you need to do is :

list<Database.saveResult> resultList = Database.update(yourlist,false);
String errorList='';
for(Database.saveResult result : resultList){
   for(Database.Error error: result.getErrors()){
      errorList += error.getMessage();

You must log in to answer this question.

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