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
RitikRitik 

Error message formatting in trigger.

Hi,

Is there any possible method by which I can format the UI of error message in a trigger?

Scenerio:
I have to display the error when a person clicks "Save" (standard button) on a Custom Object's standard edit page layout.
For this I have written a trigger (trigger.New.addError(......)).
Here, I have to display a table (which will show all the values coming from a list.)
If not possible, I have to display values of atleast one field in different rows.
For this I have written

errorMsg = 'Following possible duplicates are present: ';
for(integer i=0 ; i < myListVar.size(); i++){
          errorMsg +=  myListVar[i].name + '\n';
}
Trigger.new[0].addError(errorMsg);


But it is not working. The whole text is coming in one line. (Although in debug log it is coming in different lines.)
Instead of \n I have also tried \r\n.

So, please tell me a method by which I can display the error mesaage in a better way.
If possible, I also want a hyperlink in names in error  message.

 
Thanks,
Ritik
Best Answer chosen by Ritik
RAM AnisettiRAM Anisetti
Hi Ritik,

Please modify your code like below

errorMsg = 'Following possible duplicates are present: ';
for(integer i=0 ; i < myListVar.size(); i++){
          errorMsg +=  myListVar[i].name + '\n';
}
Trigger.new[0].addError(errorMsg,false);
=====================================================

please try this one for hyper link

<a style="text-decoration: none;" href="/'+myListVar[i].id+'">Name</a>




 

All Answers

RAM AnisettiRAM Anisetti
Hi Ritik,

Please modify your code like below

errorMsg = 'Following possible duplicates are present: ';
for(integer i=0 ; i < myListVar.size(); i++){
          errorMsg +=  myListVar[i].name + '\n';
}
Trigger.new[0].addError(errorMsg,false);
=====================================================

please try this one for hyper link

<a style="text-decoration: none;" href="/'+myListVar[i].id+'">Name</a>




 
This was selected as the best answer
RitikRitik
Hi Ram,

Thanks for replying. 
Your solution for hyperlink is working perfectly fine. :)
But, error message is still coming in one single line. i.e. Functionality of \n is not working.
Please help.


Regards,
Ritik
RitikRitik
Hi Ram,

I was just playing around with the code and I have found out that with, and it is setting escape="false" I can use most of the HTML tags. SO instead of \n now I have used <br/>, and it is working perfectly fine.
Just wondering if I can get some more information on escape= true/false. i.e. Why and in which all conditions we can use escape attribute.
Thanks again.

Code Snippet, which I have used:
            errorMsg = 'Following possible duplicates are present: ' + '<br/>';

            errorMsg += '<a href=\'/'+ myListVar[i].Id +'\'> '+ myListVar[i].Name + '</a>';

            Trigger.new[0].addError(errorMsg,false);

Regards,
Rumit