Enforce Customer ID Format in Salesforce: Country Code + 8-digit Number

Ensure every Account customer ID follows the pattern XX-12345678 (2-letter billing country code, dash, 8 digits). Use a custom field, a formula for billing country code, and a validation rule to enforce format and uniqueness.

Business requirement

Each customer record must have a unique Customer ID that follows a strict pattern so it can be synchronized with external systems. The required format is the 2-character billing country code, a dash, and an 8-digit number — for example: US-12345678. The Customer ID must be exactly 11 characters with no spaces.

Solution overview

This solution uses declarative Salesforce components: a custom text field to store the Customer ID, a formula field to extract the billing country code, and a validation rule to validate format, length, and country-code match.

Steps to implement

  • Create a custom Text field on Account named Customer_ID__c with length 11 and set it to be unique.
  • Create a formula field Billing_Country_Code__c (Text) that returns the billing country code from the address.
  • Add a validation rule on Account to enforce the format and ensure the country code matches the billing country code.

Formula field

Create a formula field (Text) to capture the billing country code:

Billing_Country_Code__c = TEXT(BillingCountryCode)

Validation rule

Use the following validation rule to enforce that when a Customer ID is entered it must match the billing country code and follow the pattern of two uppercase letters, a dash, and eight digits:

AND(
    NOT(ISBLANK(Customer_ID__c)),
    OR(
        LEFT(Customer_ID__c, 2) != Billing_Country_Code__c,
        NOT(REGEX(Customer_ID__c, "^[A-Z]{2}-\\d{8}$"))
    )
)

Explanation:

  • NOT(ISBLANK(Customer_ID__c)) — only validate when a value is provided.
  • LEFT(Customer_ID__c, 2) != Billing_Country_Code__c — ensures the first two characters match the billing country code.
  • NOT(REGEX(…)) — enforces the pattern: two uppercase letters, a hyphen, and eight digits.

Error message

Set the validation rule error message to:

Please add Customer ID in proper format: 'XX-NNNNNNNN' where 'XX' is the 2-character billing country code and 'N' is an 8-digit number.

Best practices & considerations

  • Make the Customer_ID__c field unique to prevent duplicates across Accounts.
  • Consider whether some customers legitimately have no Customer ID — the validation rule above allows blank IDs and only enforces format when a value is provided.
  • If your BillingCountryCode values are not two-letter ISO codes, adjust the formula field or validation accordingly.
  • Test with variations (different countries, lowercase input) — REGEX above requires uppercase; you can wrap Customer_ID__c with UPPER() in the regex check if you want to accept lowercase entries and normalize them.

Why this matters for Salesforce admins and developers

Enforcing a consistent, predictable Customer ID format strengthens data quality and makes integrations with external systems reliable. This approach is declarative, maintainable, and auditable — ideal for admins and developers who want low-code solutions that prevent downstream synchronization errors.