apex addError - remove default error message

2019-08-18 11:47发布

问题:

I have added addError to my record like in the following. v.addError('My Error message');

But I get the error message like in the following.

I don't want the default part "Error: Invalid Data. Review all error messages to correct your data".

I tried adding to some field instead of adding it to the whole record. But in this case, it implies that the error is specific to what the user has entered in that field. But this is not what I want. My error is record specific.

In a nutshell, based on some condition, I want to stop a record being inserted. This is actually to be added to before insert of my trigger.

Please help.

回答1:

Try this Code for field level error message:

trigger AccountTrigger on Account (before insert) {
    for(Account accIns  :trigger.new){
        if(accIns.Rating == 'Hot'){
            accIns.Rating.addError('My Error message');
        }
    }
}