function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DiguDigu 

Phone Numbers Formatting

I have various phone number formats , such as 

1. +1 (234)-(345)-(3456)
2. 2343453456
3.+45 (234)-(345)-(3456)
4. Some are not even 10 digit numbers . (234-3456)

on Account object . 

Do you have any idea how to format , all these different formats, into one single format . 

(234)-(345)-(3456)

I am thinking of executing a piece of code, through Console . Any sample codes will be appreciated . 

Thanks !! 
Digu 
Swayam@SalesforceGuySwayam@SalesforceGuy
Hi,
Below is a static method that will reformat a phone number. The method removes characters, and it will check for strings of numbers that are 10 or 11 digits long. The logic would need to be changed to process international numbers.
private static String FormatPhone(String Phone) {
  string nondigits = '[^0-9]';
  string PhoneDigits;
  
  // remove all non numeric
  PhoneDigits = Phone.replaceAll(nondigits,'');
  
  // 10 digit: reformat with dashes
  if (PhoneDigits.length() == 10) 
    return PhoneDigits.substring(0,3) + '-' +
           PhoneDigits.substring(3,6) + '-' +
           PhoneDigits.substring(6,10);
  // 11 digit: if starts with 1, format as 10 digit 
  if (PhoneDigits.length() == 11) {
    if (PhoneDigits.substring(0,1) == '1') {
      return  PhoneDigits.substring(1,4) + '-' +
              PhoneDigits.substring(4,7) + '-' +
              PhoneDigits.substring(7,11);
    }
  }

  // if it isn't a 10 or 11 digit number, return the original because
  // it may contain an extension or special information
  return( Phone );
}

You can query on account and pass phone no. to this , it will return a string and you can update the phone number,

Hope this will help, let me know in case of any doubt

--
Thanks,
Swayam
@salesforceguy
 
Nhan Nguyen NgocNhan Nguyen Ngoc
Hi Swayam,

Yes, we can update the unformated phone number (not 10 digit numbers) but we can NOT "click to call". Seemly "click to call" feature does not execute unformatted phone number. Right? For example, it does not work with short extension with 5 digit numbers.

Thanks,
Nhan