3

Whenever a record is created by user (using New button on related list), "before insert before update" trigger validates a look up field to see if the value entered already exists in Salesforce (e.g. zip code).

When user enters a zip code in "zip code customer moving to" field and if that does not exist in SFDC, error is displayed at the top mentioning as shown in the image:

Error Message

2 things need to be done:

  1. How can I customize the error message to show only "Error: Invalid zip entered" removing the verbiage such that "Invalid data. Review all error ....."

  2. This error comes from trigger. How can i display the error below "Zip code Customer moving to" field?

0

2 Answers 2

12

You can add an error to a specific field using the addError method as follows:

SObject someRecord;
someRecord.SomeField__c.addError('Custom Message');
//or
someRecord.someField__c.addError(someException);

Note that you cannot add it to a field dynamically. This error mapping can only be done with a hard-coded field. See also:

4

You can call addError on the field directly:

record.Zip_Code_Moving_To__c.addError('Invalid Zip Code Entered');

This will associate the error to the field, and it will highlight the field with red box and the error message underneath.

The red text at the top ("Review all error messages...") can't be altered or removed. This allows users to know there's an error, even if there's many hundreds of fields on the page and they have to scroll down.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .