Enforcing Customer ID Format in Salesforce

Enforce a strict Customer ID format (e.g., US-12345678) on Account records using a unique text field, a billing country formula, and a validation rule to ensure format and length.

Overview

When integrating Salesforce with external systems, consistent and validated identifiers are essential. This guide shows a simple, declarative approach to enforce that Customer ID values follow the pattern: two-letter billing country code, a dash, and an 8-digit number (example: US-12345678). The field must be exactly 11 characters long with no spaces.

Steps

  • Create a unique Text(11) field on Account to hold the Customer ID.
  • Add a formula field to derive the Billing Country Code from the Account address.
  • Create a validation rule that checks both the format and that the country code in the Customer ID matches the billing country code.

1) Custom Field

Create a custom field on Account:

  • Field Type: Text
  • Length: 11
  • Unique: True

2) Formula Field for Billing Country Code

Create a formula field Billing_Country_Code__c (Text) to capture the two-letter billing country code:

Billing_Country_Code__c = TEXT(BillingCountryCode)

3) Validation Rule

Create a validation rule (Customer_ID_Format) on Account to enforce format and country-code match. The rule should only run when Customer_ID__c is not blank:

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 of the checks:

  • (LEFT(Customer_ID__c, 2) != Billing_Country_Code__c) — Ensures the first two characters match the Account Billing Country Code.
  • NOT(REGEX(…)) — Ensures the value follows uppercase two letters, a dash, and 8 digits. Together with the field length and no allowed spaces, this enforces an exact 11-character pattern.

Error message suggestion: Please add customer Id in proper format: ‘XX-NNNNNNNN’ where X is the 2-character billing country code and N is an 8-digit number.

Best Practices & Notes

  • If some Accounts may not have a Customer ID, keep the field optional and rely on the validation rule to only fire when a value is provided.
  • Ensure BillingCountryCode is stored in ISO 3166-1 alpha-2 format (two uppercase letters). If your org uses different country formats, consider normalizing the billing country code before matching.
  • Test the validation rule with different country codes and edge cases (lowercase letters, missing dash, extra spaces) to confirm correct behavior.
  • Consider adding a UI help text on the field to show the expected format to users.

Why this matters

Standardizing Customer IDs prevents integration failures, reduces data cleanup, and improves downstream reporting and reconciliation. Enforcing the country code in the ID ensures the identifier is meaningful across systems and avoids mismatches when syncing records.

For Salesforce admins and developers, this declarative pattern is easy to implement, maintain, and test. It provides strong, readable validation rules without the need for Apex triggers or Flow complexity unless you require advanced normalization or automated generation of IDs.