Sub topic 7
Error messages and where they appear
This chapter assumes you can create a validation rule in Setup and write a basic error condition formula. You should also know the difference between a record page layout and the fields on an object. The formula decides whether a save fails. The error message and error location decide what the user sees when it does, and that determines whether they can fix the problem without asking an admin.
The two display settings
Every validation rule has two settings that control the failure experience:
- Error Message is the text shown to the user.
- Error Location is where the text appears. You choose Top of Page or Field. When you choose Field, you also pick one field from the object.
Both settings are required. You can edit them at any time, and changing them does not affect which records fail.
Writing messages users can act on
A message is read by someone in the middle of a task. They need to know what is wrong and what to do next. A good message names the field in plain language and states the rule and the fix.
| Weak message | Better message |
|---|---|
| Invalid data. | Enter an Amount greater than zero before moving to Negotiation/Review. |
| Error 1042 | Billing Postal Code must be 5 digits or ZIP+4, for example 94105 or 94105-1234. |
| You cannot do that. | Only users with the Opportunity Amount Editor permission can change Amount. Ask your manager to request access. |
Some practical guidelines:
- Lead with the action ("Enter...", "Select...", "Remove...").
- Use the field labels users see on the page, not API names such as
Billing_Postal_Code__c. - Include an example of a valid value when the rule checks a format.
- Say who to contact when the user cannot fix the problem themselves, as in the third row above.
- Keep it to one or two sentences. The field has a limit of 255 characters, and long messages wrap badly beside a field.
Messages are static text
The message cannot contain merge fields or formulas. You cannot write "Amount of {!Amount} is too low". If you need to tell the user which value failed, name the field and state the threshold in the text.
If you need different wording for different failures, write separate rules. Each rule has one condition and one message, which also makes testing and troubleshooting easier. A single rule with a long OR(...) condition gives the user a vague message because it cannot say which branch failed.
AND(
ISPICKVAL(StageName, "Negotiation/Review"),
OR(ISBLANK(Amount), Amount <= 0)
)This is the Opportunity rule from earlier chapters. It has two failure causes in one condition, so the message must cover both: "Amount must be filled in and greater than zero when the stage is Negotiation/Review." That wording is accurate for either cause.
Translation
If your org supports several languages, enable Translation Workbench. It lets you translate validation rule error messages. Write the source message in your org's default language first, and keep it simple, because short sentences translate more reliably.
Choosing the error location
Top of Page
A page-level error appears in a banner at the top of the record form, or in the error list of the edit dialog. It is not tied to any field. Use it when:
- the rule compares several fields and no single one is the cause,
- the failure depends on a related record or a global variable,
- the rule blocks an action, such as a stage change, and not a field's value.
Field
A field-level error appears directly under the selected field, highlighted in red. The page also shows a general notice that lists the fields to review. Use it when one field is clearly the one to correct. The user's eye goes straight to it.
For the Opportunity rule above, the best location is the Amount field. The user can see where to type a value, and the message appears next to the input.
flowchart TDThe diagram below summarizes how to choose.
Limits of field-level errors
- Only one field per rule. If two fields could be at fault, pick the one the user should edit first, or use Top of Page and name both fields in the message.
- The field must be visible to the user. If the selected field is not on the page layout, or the user lacks field-level security to see it, the message has nowhere to attach. The user may see the error without a field highlighted, or in the page-level error list. Check the behavior in your own org. The rule still blocks the save in every case.
- The field does not have to be in the formula. You can attach a message about Close Date to a different field, but this confuses users. Keep the location close to the cause.
What happens in the API, Flow, and Apex
Not every save comes from a page. Data loads, integrations, flows, and Apex also trigger validation rules, and they receive errors in different forms.
- API and Data Loader: The error is returned with the failed row. The message text is the same. If you chose Field, the response also names that field. Page-level errors come back with no field.
- Apex:
Database.insert(record, false)returns aDatabase.SaveResult. EachDatabase.ErrorhasgetMessage(),getStatusCode(), andgetFields(). For a rule with a Field location,getFields()contains the field's API name. For Top of Page, it is empty. A plaininsertorupdatethrows aDmlExceptionwhose message contains the same text. - Flows: A record-triggered flow that fails because of a validation rule shows the rule message in the flow's fault path or error email.
This means your message must make sense with no screen around it. "Fix the highlighted field" is meaningless to an integration engineer reading a log. "Amount must be greater than zero when Stage is Negotiation/Review" works in both places.
Checking the location from a test
You can verify both the text and the location in an Apex test. It assumes the Opportunity rule above is active with Error Location set to Amount.
@IsTest
private class OpportunityErrorLocationTest {
@IsTest
static void errorAttachesToAmount() {
Opportunity opp = new Opportunity(
Name = 'Test Opp',
StageName = 'Negotiation/Review',
CloseDate = Date.today().addDays(30)
);
Test.startTest();
Database.SaveResult result = Database.insert(opp, false);
Test.stopTest();
System.assert(!result.isSuccess(), 'Insert should fail validation');
Database.Error err = result.getErrors()[0];
System.assertEquals(
StatusCode.FIELD_CUSTOM_VALIDATION_EXCEPTION,
err.getStatusCode()
);
System.assertEquals(
new List<String>{ 'Amount' },
err.getFields(),
'Error should be attached to Amount'
);
System.assert(
err.getMessage().contains('Amount'),
'Message should name the field'
);
}
}Asserting on the full message text makes tests brittle, because any wording edit breaks them. Asserting that it contains the field label is a reasonable compromise.
Several rules failing at once
All active rules on the object are evaluated on a save, and the user sees the messages from every rule that fails. They do not have to fix one error, save, and discover the next. This is another reason to keep one condition per rule. Two small rules produce two precise messages, each next to its own field.
Messages from multiple page-level rules appear stacked. Field-level errors appear on their own fields. Check the order and wording by creating a test record that breaks several rules at once.
Common mistakes
- Writing "Invalid value" with no field name or fix.
- Putting a field-level error on a field that is not on the layout.
- Using API names in the message text.
- Combining unrelated checks in one rule so the message must be generic.
- Choosing Top of Page for every rule, which forces users to match the message to a field by reading.
- Never testing the message as a non-admin user.
Exercise
A rule blocks saving an Account when Billing Country is USA and Billing Postal Code is not 5 digits or ZIP+4. Write an error message, choose an error location, and give one reason for your choice. Then name one case where the user might not see the field-level error beside the field.
Show answer
Message: "Billing Postal Code must be 5 digits or ZIP+4, for example 94105 or 94105-1234." Location: Field, set to Billing Postal Code. Reason: one field is clearly the cause, so the message appears right beside the input the user must correct. The user might not see the error beside the field if Billing Postal Code is missing from their page layout or hidden from them by field-level security. In that case the rule still blocks the save, so test as a user from each affected profile.
Exercise
An Apex test calls Database.insert(record, false) on a record that fails a rule whose Error Location is Top of Page. What does getFields() return on the first error?
Show answer
An empty list. A field is only listed when the rule's Error Location is set to Field.