Salesforce | Display error message returned by Trigger with enter key

|
| By Webner

How to send an error message from trigger back to the layout and display it on the record detail page. You need to use addError method for this.

For example :

errorMsg=’Some of the catalog courses are not available in LMS.<br/>Please sync them to LMS first’;
for( Account account: trigger.new ){
   account.addError(errorMsg);
}

It will show on the top of the page as :

1

But we also need to add new line character to this message. For this, you need to add in the message and send extra parameter in the addError method like :

errorMsg=’Some of the catalog courses are not available in LMS.<br/>Please sync them to LMS first’;
for( Account account: trigger.new ){
   account.addError(errorMsg,false);
}

Now it will look like this :

2

5 comments

  1. It helped, Thanks. But we show the error on top of the field instead of showing it on the top of the page.

      1. Yes, it is possible. You will have to add error on the specific field.
        For example, Trigger.new[0].myField__c.addError(‘badvalue’);
        In the above example, add error as “account.customField__c.addError(‘Error Message’);”

Leave a Reply

Your email address will not be published. Required fields are marked *