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
PremanathPremanath 

How to write apex class for formated phone number

Hi All,

 

I have some issue like-- > I have two fields

                                                  phone number-- +91-(08)/9855664555

                                                 country code ---> 110 

 

 third field is should be automatically comes when we fill above two fields like this :

                                  formatedph: 11091089855664555

 

Means write a wrapper class it call from any where(trigger) . and print without special char.

 

Thank you guys,

JitendraJitendra

Hi,

 

Cant you use simple formula field and replace special characters with blank using "substitute" method of TEXT.

BritishBoyinDCBritishBoyinDC

The code below strips out all non digits and formats to 10 digits if 10 long, otherwise returns original

 

But you can just use the regex/replace to give you back a digits only number:

String fphone = cphone.replaceAll('\\\\D','');


public static String formatphonenumber(String cphone) {
String fphone = cphone.replaceAll('\\\\D','');

if (fphone.length() == 10) {
fphone = formatphonenum(fphone);
return fphone;
}
else {
return cphone;
}

}

static String formatphonenum (String s) {
s = '(' + s.substring(0, 3) + ') ' + s.substring(3, 6) + '-' + s.substring(6);
return s;
}

ANIL KUMAR REDDY MANNURUANIL KUMAR REDDY MANNURU
s = '(' + s.substring(0, 3) + ') ' + s.substring(3, 6) + '-' + s.substring(6);-----> If you use this line format is little bit different small change is required in it. The format we get like (000)-00-0-0000.

public String getFormattedPhoneNum() {
        if(phoneNumber == null){
            return '';
        }
        else{
            String s = '(' + phoneNumber.substring(0, 3) + ')' + phoneNumber.substring(3, 6)  + phoneNumber.substring(6);
            return s;
        }

Result:(000)-000-0000