User-facing message written as a literal
ID |
apex.maintainability.hardcoded_messages |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
I18N |
Language |
Apex |
Description
Reports a literal string passed to addError or to ApexPages.addMessage, both of which put the text straight in front of a user.
Rationale
A message written in the code ships in one language and changes only with a deployment. Every org that installs the package sees the English text regardless of the user’s locale, and the support team cannot correct a wording mistake without a release. Custom Labels solve both: they are translatable per language and editable in setup, and the compiler still checks the reference.
The following code illustrates the pattern detected by this rule:
public class MessageSources {
public void validate(Account a) {
// FLAGGED: User-facing message written as a literal
a.addError('Account name is required');
// FLAGGED: User-facing message written as a literal
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Account name is required'));
a.addError(Label.Account_Name_Required);
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, Label.Account_Name_Required));
}
}