Skip to content

Instantly share code, notes, and snippets.

@apsolut
Created August 10, 2023 08:39
Show Gist options
  • Save apsolut/f085950945286341f22229e7e6f05203 to your computer and use it in GitHub Desktop.
Save apsolut/f085950945286341f22229e7e6f05203 to your computer and use it in GitHub Desktop.
SF phone number alignment
SELECT Id, Phone,
CASE(
/* Check if phone number contains a country code */
IF(
REGEX(Phone, '^[+](.*)$'),
/* If phone number already contains country code, format it */
REGEX(
SUBSTITUTE(Phone, ' ', ''),
'[\\+]', '00'
),
/* If phone number doesn't contain country code, add country code based on record's country */
CASE(
BillingCountryCode,
/* If BillingCountryCode is 'US', add '+1' */
'US', '+1' + REGEX(SUBSTITUTE(Phone, ' ', ''), '[\\D]', ''),
/* If BillingCountryCode is 'DE', add '+49' */
'DE', '+49' + REGEX(SUBSTITUTE(Phone, ' ', ''), '[\\D]', ''),
/* Add more cases for other countries as needed */
/* Default case, add '+00' */
'+00' + REGEX(SUBSTITUTE(Phone, ' ', ''), '[\\D]', '')
)
),
/* Default value for NULL or empty Phone */
''
) AS FormattedPhone
FROM Account
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment