2

In before insert trigger I need to verify couple of validations for which I have written 2 methods - Method1(casesList) and Method2(casesList). Both methods operate on the same list of cases in before trigger context and adds different error messages say error1 and error2 respectively by using addError API.

Now order of execution for these methods --

beforeInsert() {

    Method1(casesList)

    Method2(casesList)

}

Now method2 overrides the error message applied by method1 on the same record. how can I get 2 error messages on the same record without overriding the previous one?

3 Answers 3

2

You will need to concatenate all the error messages and add it at once using addError method.

As you are passing list of records as parameter to your methods in before insert context, you can create a map for storing errors where you can use index as key and concatenation of error message as value. (Lists are ordered collection and Id's are not available in before insert context)

  • Create a map of Map<Integer,String>
  • Populate the map with error messages in your first method with index as key and error message as value
  • In your second method, check if that same record exists in your map using index, if present, append the error in map, if not add error message in map.
  • Loop through Map<Integer,String>, check for non empty error message and addError to Trigger.New using index.
0
0

I am just running of the assumption that method2() will be called after method1() as per your question, you concatenate the error messages as a string and add the error message in the method2.

concatenatedstring = '';

Method1(casesList){
concatenatedstring += 'my first error method /n';
}
Method2(casesList){
concatenatedstring += 'my second error method /n';
eadderror(concatenatedstring);
}

Update:

  1. I relooked into the code and realized the method1() and method2() are accepting list params, so my previous code is absurd as adrian pointed out.
  2. This should be after insert in my opinion ( if you want this to be before insert then you can modify the key by adding other info like caseinfo, casenumber etc,, and make the key unique in the error map. Make sure to check if the field that you are using to generate the key is not null or even better form the key using a required field) because you need to know the id of the record to associate the errors to the account, Id is not available in a before insert context.

I created a small POC in my dev box with account rewrite your code for your usecase/object.

trigger beforeinserttest on Account (after insert) {


accounttrighandler ac_handler = new accounttrighandler();

    ac_handler.method1(trigger.new);
    ac_handler.method2(trigger.new);
}

public class accounttrighandler{
    public map<string,string> errormap = new map<string,string>();

    public void method1(List<account> acc1){
        for(account a1: acc1){
          if(!errormap.containsKey(a1.id)){
          errormap.put(a1.id,'METHOD 1: adding error 1 '+a1.id);
          }
        }
    }

    public void method2(List<account> acc2){
        for(account a2: acc2){
            if(errormap.containsKey(a2.id)){
                    a2.adderror('Account '+a2.id+ ' contains errors ' + errormap.get(a2.id) + ' METHOD 2: adding erorr 2 '+a2.id);
                }
            }
        }
}
2
  • 1
    Please note you can get <ol> formatting by writing 1. instead of 1).
    – Adrian Larson
    Nov 13, 2017 at 19:50
  • Thanks for the reply Rao. AfterInsert trigger i dropped the idea as it would insert my record first in the database and then throw the validation error. I want stop it before even commiting to database.
    – Prasadium
    Nov 15, 2017 at 18:58
0

For future wanderers - you can run addError() on a particular field of a record and messages from different fields don't override each other.

You must log in to answer this question.

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