function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Can I embed html in the message of a Custom Exception?

I have a custom Exception with a custom Message defined. I would like to send in an HTML string to MyDateRangeException (code below) for some special formatting and so that it might contain some links. Is that possible?

(I am using this exception from a trigger, so I don't have the option of doing anything in the page. Also, cannot use addError because this message will not necessarely refer to the current record being inserted or updated).

public with sharing class MyDateRangeException extends Exception {
    public MyDateRangeException(String badRangeDates, String moreInfo) {        
        this.setMessage((moreInfo == null?'':moreInfo)+' A date range was not met: '+ badRangeDates);
Daniel BallingerDaniel Ballinger
Just my personal opinion, but I'd avoid directly putting HTML into the Exception message due to the potential for security problems. A malicious person could create bad data that will inject JavaScript etc... into the resulting page.

Depending on how you are handling the exception, maybe you could add additional properties to the custom exception that expose the required URLs. You could then use custom Visualforce to display those URLs as links in a safer way.
Thank you for your reply. The message will need to appear on the standard SF page, which means I can't do anything in VF to display the URLs. 
Sounds like what I am trying to do might not be possible nor desirable. 
Hi NinetaM,

You can add link in your message,However when you need to display the message in VF page you need to add your code as below .
<apex:pageMessages escape="false"/>
After this you can add code in your error message like below .
<a href="/">Link</a>
Let me know if it helps !!