0

I have a Custom Mobile Phone field where we only want to see numeric characters Is there a way to remove all text except 0 through 9? I have a Substitute formula that removes (, ), -, _ etc but we need to get rid of a-z as well Is there a Wildcard option in Substitute? Can't use Regex in a formula field and don't need a validation Rule using Regex.

3
  • 1
    Can you edit your post, what you have tried so far and where did you struck with clear explanation. Please read this salesforce.stackexchange.com/help/how-to-ask before posting a question. Apr 11, 2022 at 2:22
  • Best if you export out all the data from that particular Object and Field into CSV format. Then do the removal using Find All & Replace All on Excel. Then once done import it back as Update. It's as simple as that
    – compski
    Apr 12, 2022 at 3:22
  • Thanks, yes that would work but isn't the automation we need :-) Apr 13, 2022 at 4:14

2 Answers 2

0

If I have understood your requirement correctly then this is my solution:

Phone field in Salesforce by default will accept any characters with maximum of 40 characters. Because of this flexibility, user can enter any data and things can turn out to be messy. Thankfully we have Regex, which allow only Numbers and Special Characters.

The below validation rule will only allow numbers to be filled in the Phone field, but you can modify it according to your need.

NOT(REGEX( Phone , "^[0-9_]*$"))

REGEX: https://help.salesforce.com/s/articleView?id=000334073&type=1

3
  • Thanks for that but a Validation rule is not what I need here and I can't use Regex in a formula unless I use Apex, which is possibly where this will end up. Trying to control what gets to Salesforce via an API and I have no control over the data being entered Apr 12, 2022 at 3:12
  • Okay. I didn't know that it is coming from an external application. Well in that case you can write the logic on the received payload in the API callout code only before saving it in the Salesforce database. Apart from that you can also store the data in a staging table, a replica of the target object and then write a batch class to move data from staging object to target object.
    – Night Guy
    Apr 12, 2022 at 8:09
  • Thanks, yes, we are looking at Apex as the only solution unless we can control the input. I was hoping to use a Flow but looks like Apex is the only way forward Apr 13, 2022 at 4:15
0

If you need to use Apex, then I would use String.replaceAll with a regular expression that matches non-digit characters (\D).

String phone = '(555) 867-5309';
final String nonDigits = '\\D'; //any non-digit character
String sanitized = phone.replaceAll(nonDigits, '');

System.assertEquals('5558675309', sanitized);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .