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));
    }
}

Remediation

Move the text to a Custom Label and reference it, e.g. a.addError(Label.Account_Name_Required). Where the message needs values interpolated, use String.format(Label.X, new List<String>{…​}) so the translated string keeps control of word order.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved