Lalit Bhatt
ServiceNow Employee
ServiceNow Employee

There are two field types: "Phone Number" and "Phone Number (E164)" to use phone numbers on the platform.

"Phone Number" is a legacy data type which inherits from String whereas "Phone Number (E164)" is the newer data type that provides richer functionalities and supports phone numbers in multiple country codes.  Use the newer data type "Phone Number (E164)" whenever you want to add a custom phone number field in a table.

 

However, This is specific only for normal fields and not for variables as a similar type does not exist for variables.

 

To perform a similar validation on catalog variables, I have used undocumented API 'GlideElementPhoneNumber', which i believe utilizes OOB Phone territories (sys_phone_territory) and Phone Validations (sys_phone_validate) tables.

 

Below solution may not cover all of the scenarios but it seems it covers most of the phone number validations. You can extend this solution to cover your specific use case if needed.

 

Solution:

 

1. Create single line text type variable

 

LalitBhatt_0-1673883423279.png

 

2. Catalog client script

 

LalitBhatt_1-1673883423280.png

 

 

function onChange(control, oldValue, newValue, isLoading) {

    //Type appropriate comment here, and begin script below
    if (isLoading)
        return;

    if (newValue == '') {
        g_form.showFieldMsg("phone_number", "Phone number must be provided. Please enter a valid phone number.", "error");
        return;
    }
    var regex = /^[+\d\s]{0,}$/;

    if (newValue != '' && !regex.test(newValue)) {
        g_form.showFieldMsg("phone_number", "Invalid phone", "error");
		
    } else {

        //Call script include
        var ga = new GlideAjax('global.ValidatePhoneNumberUtils'); //Scriptinclude
        ga.addParam('sysparm_name', 'validatePhoneNumber'); //Method
        ga.addParam('fullPh', newValue); //Parameters
        ga.getXMLAnswer(getResponse);

        function getResponse(response) {

            var res = JSON.parse(response);
            console.log(res);

            var valid = res.valid;
            var country = res.country;

            if (!valid)
                g_form.showFieldMsg("phone_number", "Phone number you entered for " + country + " country is not valid. Please enter number in right format.", "error");
            else
                g_form.showFieldMsg("phone_number", "You entered a valid phone number for " + country + ".", "info");
        }
    }

}

 

3. Client callable script include

 

 

var ValidatePhoneNumberUtils = Class.create();
ValidatePhoneNumberUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    validatePhoneNumber: function() {

        var fullPh = this.getParameter('fullPh');
        var obj = {};

        var gePN = new GlideElementPhoneNumber();

        obj.valid = gePN.setPhoneNumber(fullPh, true);
        obj.country = gePN.getTerritory();
        obj.globaldisplayValue = gePN.getGlobalDisplayValue();

        return JSON.stringify(obj);
    },


    type: 'ValidatePhoneNumberUtils'
});

 

 

LalitBhatt_2-1673883423280.png

 

 

Result:

LalitBhatt_3-1673883423281.png

 

LalitBhatt_4-1673883423282.png

Hope this helps!

 

Comments
BiancaK
Tera Expert

I have tried this and it does not work for me. Can you please re look at this solution?

lalit007
Tera Contributor

@BiancaK  Thanks for your comment.

I tried it again and it works. 

lalit007_0-1697542897768.png

 

Could you please share what exact issue are you facing?

prashantnagaral
Kilo Guru

Hi,

 

We tried this, working only for Admins. Not working for normal users from Service Portal.

 

Please help

Aniket Bhanse
Tera Guru

While creating the Script Include, I was asked to specify a Role for Client Callable Script Include. I selected "Admin" there. 

Also, even if I enter the wrong phone number, the Catalog item is still getting submitted.

Wilwod
Tera Expert

Made a few additions to this to keep the mandatory valuation working to stop users from submitting a wrong phone number.

 

function onChange(control, oldValue, newValue, isLoading) {

    //Type appropriate comment here, and begin script below
    if (isLoading)
        return;

    if (newValue == '') {
        g_form.showFieldMsg("phone_number", "Phone number must be provided. Please enter a valid phone number.", "error");
        return;
    }
    var regex = /^[+\d\s]{0,}$/;

    if (newValue != '' && !regex.test(newValue)) {
        g_form.showFieldMsg("phone_number", "Invalid phone number", "error");
		g_form.setValue('phone_number', '');

    } else {

        //Call script include
        var ga = new GlideAjax('global.ValidatePhoneNumberUtils'); //Scriptinclude
        ga.addParam('sysparm_name', 'validatePhoneNumber'); //Method
        ga.addParam('fullPh', newValue); //Parameters
        ga.getXMLAnswer(getResponse);

        function getResponse(response) {

            var res = JSON.parse(response);
            console.log(res);

            var valid = res.valid;
            var country = res.country;

            if (!valid) {
                g_form.showFieldMsg("phone_number", "Phone number you entered for " + country + " country is not valid. Please enter number in right format.", "error");
				g_form.setValue('phone_number', ''); }

            else {
                g_form.showFieldMsg("phone_number", "You entered a valid phone number for " + country + ".", "info");
			}
        }
    }

}

 I have added two g_form.SetValue lines to remove the wrong text where the input is incorrect. Typing in a correct phone number will not be removed, just incorrect inputs. 

Osaretin Olumek
Tera Contributor

Can you please show how to remove the international country code from the number format for E164 field. 

VishalV02239290
Tera Contributor

You can use the attribute in the dictionary

pn_display_territory_selector=false

Version history
Last update:
‎01-16-2023 08:09 AM
Updated by:
Contributors