[SalesForce] Before Delete addError() message not user friendly

I have a before delete trigger on Topic Assignment that I'm using to block a User from deleting a Topic from a Question in a lightning community. The trigger works fine and blocks the DML, however the error message is not User friendly and I cannot figure out how to get it to display ONLY my error message.

What's odd is that it seems to work for Trigger.new. Help is appreciated.

trigger TopicAssignmentTrigger on TopicAssignment (before delete) {

    for(TopicAssignment ta : Trigger.old){

        //Conditional logic will go here
        ta.addError('CANNOT DELETE THIS TOPIC');

    }

}

enter image description here

Best Answer

Actually addError() do support HTML markups to beautify your error message.

addError(exceptionError, escape)

So i give this method a try, here is my result:

Without HTML markup:

a.addError('account can not be deleted');

enter image description here

With HTML Markup:

a.addError('<h1><i>account can not be deleted</i></h1>', false);

enter image description here

There are some Con's of using this method as default addError method escapes any HTML string and here you are stopping it from doing so. So be aware of it before using it.

Be cautious if you specify false for the escape argument. Unescaped strings displayed in the Salesforce user interface can represent a vulnerability in the system because these strings might contain harmful code. If you want to include HTML markup in the error message, call this method with a false escape argument and make sure you escape any dynamic content, such as input field values. Otherwise, specify true for the escape argument or call addError(String errorMsg) instead.

Reference

Important thing to note here is that this method only works in classic, In lightning and Salesforce1 string is always escaped. :P

Related Topic