1

In contact object, I have phone number field which is type of Phone. The current value is 8767876541. The problem is it is not displaying in correct format. The format which I am looking out is (876)787-6541 . When I click on that field by manually its getting formatted into expected way. the problem is I have 2000+ contacts with wrong format and I want to update all of them through some code.

So I tried the below code for one record to see if that updates the Phone field in correct format.

List myobjects = [ Select Id,Name,Phone From contact where Id='0033600000LMk8M']; for( Contact myobject : myobjects ) { myobject.Phone = '2159003935' ; }

update myobjects;

But it didn't help me to make the phone number into correct format

I checked it through some stack exchange questions and the answer is showing as

" Any entry of phone numbers through the API does not receive formatting."

Any solution on this please

2
1

Try using the below method on the phone number:

static String formatPhoneNum (String fixedPhone) {
    fixedPhone = '(' + fixedPhone.substring(0, 3) + ') ' + fixedPhone.substring(3, 6) + '-' + fixedPhone.substring(6);
    return fixedPhone;
    }
2
  • Can you let me know how I can add it in my code please – KSL Aug 30 '16 at 21:45
  • It worked... Below is my code List<Contact> myobjects = [ Select Id,Name,Phone From contact where Id='*************']; for( Contact myobject : myobjects ) { String fixedPhone; fixedPhone = myobject.Phone; fixedPhone= '(' + fixedPhone.substring(0, 3) + ') ' + fixedPhone.substring(3, 6) + '-' + fixedPhone.substring(6); system.debug('fixedPhone ::' + fixedPhone); myobject.Phone = fixedPhone ; } update myobjects; – KSL Aug 30 '16 at 22:14

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.