How to get area code from 10 digit phone number

Praveen75
Tera Contributor

Hi Guys, I have a field 'office_phone' of type 'phone_number_e164'. When the field value is '+49 123 xxxxxxx, I would like to get '123' alone (basically to get area code). How to get this, because sometimes the number can be '+1 123 xxxxxxx'. Please suggest. Thanks. 

3 REPLIES 3

ChiranjeeviR
Kilo Sage

Please follow below steps.


1) Have the mapping of the Country and Country codes in a lookup table (could be country table itself).


2) Create an Onchange Catalog client script.


3) Pick the value of the country from country field, then pass it on to the server with glide ajax and then from server return the country code (through script include).


4) Use g_form.setValue to append the country code returned from server to the phone number field.

 

If my answer solved your issue, please mark my answer as👍 Correct &Helpful based on the Impact.

Thanks & Regards,
Chiranjeevi
ServiceNow Developer | | ITSM | | ServiceNow Discovery | | Event Management | | Service Mapping | | CMDB

Please mark as Correct Answer/Helpful, if applicable.

Sandeep Rajput
Tera Patron
Tera Patron

@Praveen75 Simply use the following script to get the number.

 

 

var phoneNumber = '+49 123 xxxxxxx';
var phoneNum = phoneNumber.split(' ')[1];
gs.info(phoneNum.trim());

 

Please mark this answer helpful and correct if it addresses your question.

Minor comment: A better name for the variable above is areaCode instead of phoneNum, as the OP asked about just getting the area code in that format.