1

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

  • 1
    Each part of Salesforce handles errors in whatever way it handles it. Some are... well, less elegant than others. There's nothing you can do to fix this. – sfdcfox Jun 10 '18 at 6:15
1

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

  • 1
    Hey, Frodo, they were asking about a Lightning Community, so I"m not sure your answer applies here. This answer is what this question deserves, but not the answer it needs right now. – sfdcfox Jun 10 '18 at 14:38
  • @sfdcfox ohh I overlooked that minor detail but I think this information could still help people also I don't see any creative solution is going to come for this question and In some manner this answer still increase OPs knowledge about this littile method so wanted to keep this answer. – Mr.Frodo Jun 10 '18 at 15:17
  • Yeah, unfortunately I tried that and it does not work in a Lightning Community -- which is what I need. I think this is a Salesforce bug, because the error message DOES display correctly with Trigger.new context – M Dev Jun 10 '18 at 20:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.