Created
August 10, 2023 08:39
-
-
Save apsolut/f085950945286341f22229e7e6f05203 to your computer and use it in GitHub Desktop.
SF phone number alignment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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