I want to take all the phone numbers for a given contact in from a SOQL query (which provides the phone number in a raw format) and then have all of the phone numbers in this format as options in a drop down:

<display name of the phone field>: <formatted phone number>

I have figured it all out except for one thing: I haven't found a standard way of formatting phone numbers.

Is there a way to invoke Salesforce's phone number formatter (kind of like Date.format()) so that I can run the incoming raw phone strings through that before concatenating it with the display name?

I understand it is possible to write my own method that would do this, but I would much rather use a standard formatter than to homebrew up my own. Using standard stuff is much better for locale adjustments and overall maintenance of the code.

  • Don't know if you'd be able to find a reference or prove it but 90% sure the answer is no.
    – Adrian Larson
    May 5 '20 at 21:19
  • I guessed that may be the case by the lack of any evidence of it being available on forums and such, but I figured I would ask and see if someone could surprise me. May 5 '20 at 21:23
  • 1
    I guess it depends on the context where you want to do this. There are wheezes where you perform in-Apex invocation of Visualforce elements, such as outputText and others to get stuff formatted, but it must be invoked in Apex in the context of a VF page...
    – Phil W
    May 5 '20 at 21:48
  • 1
    The format function is baked in to the UI elements, not the API or backend (you can prove this by using the data loader to insert records). You would definitely need to write your own formatter if you were interested in having one available. You can see the rules for number formatting in the Help; it's actually very limited; only for US and Canada, and only for 10-digit numbers and 1+10 digits.
    – sfdcfox
    May 5 '20 at 22:37

Probably not what you want but

String s = UserManagement.formatPhoneNumber('1','6505551212');
String s = UserManagement.formatPhoneNumber('1','(650) 555 1212');

both produce +1 6505551212

reference to UserManagement class

  • That wasn't exactly what I was looking for (I was hoping to see it get formatted the same as the UI shows it), but it is much better than anything else I have seen out in the wild, so a +1 to you from me. May 6 '20 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.