Need to add validation on phone number

MK-p
Tera Contributor

I have a filed called phone number on catalog which display on catalog task with mandatory field.

I want to add validation like if phone number exists in cmdb_ci_comm table and assigned to anyone then alert message like number already exists. Can someone help me with the code.

3 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@MK-p 

something like this

Client script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }
   g_form.hideFieldMsg('phone_number');

    var ga = new GlideAjax('PhoneNumberValidation');
    ga.addParam('sysparm_name', 'checkPhoneNumber');
    ga.addParam('sysparm_phoneNumber', newValue);
    ga.getXMLAnswer(function(response) {
        if (response === 'true') {
            g_form.showFieldMsg('phone_number', 'This phone number already exists and is assigned to someone.', 'error');
            g_form.clearValue('phone_number');
        }
    });
}

Script Include: Client callable

var PhoneNumberValidation = Class.create();
PhoneNumberValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    checkPhoneNumber: function(phoneNumber) {
        var gr = new GlideRecord('cmdb_ci_comm');
        gr.addQuery('phone_number', this.getParameter('sysparm_phoneNumber'));
        gr.addNotNullQuery('assigned_to');
        gr.query();
        if (gr.next()) {
            return 'true';
        }
        return 'false';
    },

    type: 'PhoneNumberValidation'
});

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

Juhi Poddar
Kilo Patron

Hello @MK-p 

You can meet this requirement by writing an onChange client script on Catalog with field phone number.

onChange catalog Client script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return; // Skip validation during form load or when the field is cleared.
    }

    // Perform a GlideAjax call to validate the phone number
    var ga = new GlideAjax('CheckPhoneNumber'); // Replace with your Script Include name.
    ga.addParam('sysparm_name', 'validatePhoneNumber');
    ga.addParam('sysparm_phone', newValue);
    ga.getXMLAnswer(function(response) {
        if (response === 'exists') {
            alert('This phone number already exists and is assigned to someone.');
            g_form.addErrorMessage('This phone number is already in use.');
            g_form.setValue('phone_number', ''); // Clear the field if validation fails.
        }
    });
}

Here 'CheckPhoneNumber' is the script include name.

'validatePhoneNumber' is the function name in script include.

Script include function:

validatePhoneNumber: function() {
        var phoneNumber = this.getParameter('sysparm_phone');
        var gr = new GlideRecord('cmdb_ci_comm'); // Communication table.
        gr.addQuery('phone_number', phoneNumber); // Replace 'phone_number' with the actual field name.
        gr.addQuery('assigned_to', '!=', ''); // Check if assigned_to is not empty.
        gr.query();

        if (gr.next()) {
            return 'exists';
        }
        return 'not_exists';
    },

Note: script include should be client callable.

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

View solution in original post

Runjay Patel
Giga Sage

Hi @MK-p ,

 

Follow below steps to perform the validation.

  1. Create onChange catalog client script and check the checkbox for tax.
  2. RunjayPatel_0-1735548968442.png

     

  3. Use below script in client script.

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }


    var ga = new GlideAjax('MobileInventory');
    ga.addParam('sysparm_name', 'checkNumber');
    ga.addParam('sysparm_number', newValue);
    ga.getXML(ValidateNumber);

    function ValidateNumber(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");

        if (answer == 'true') {
            alert('Mobile number already exist');
            g_form.clearValue('mobile_number');

        }
    }
    //Type appropriate comment here, and begin script below

}

 

 

  • Now create a script include and make that client callable, Use below script in it.

 

 

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

    checkNumber: function() {
        var flag = false;
        var number = this.getParameter('sysparm_number');
        var gr = new GlideRecord('cmdb_ci_comm');
        gr.addEncodedQuery('assigned_toISNOTEMPTY^phone_number=' + number);
		gr.query();
        if (gr.next())
            flag = true;

        return flag;
    },



    type: 'MobileInventory'
});

 

 

 

I have check this in my pdi, it working as expected.

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

View solution in original post

In this video i have explained about Web service integration in ServiceNow like how it works, how we can configure it, what are the prerequisite and many more. I have covered below topics in this video. 1. understand Web Service. Like when and how we will use it. 2. Talked about Inbound and ...
5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@MK-p 

you can use onChange catalog client script which Applies on Catalog Task

Use GlideAjax and query that cmdb_ci_comm table and search in phone number

What did you start with and where are you stuck? It's an easy requirement.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

@MK-p 

something like this

Client script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }
   g_form.hideFieldMsg('phone_number');

    var ga = new GlideAjax('PhoneNumberValidation');
    ga.addParam('sysparm_name', 'checkPhoneNumber');
    ga.addParam('sysparm_phoneNumber', newValue);
    ga.getXMLAnswer(function(response) {
        if (response === 'true') {
            g_form.showFieldMsg('phone_number', 'This phone number already exists and is assigned to someone.', 'error');
            g_form.clearValue('phone_number');
        }
    });
}

Script Include: Client callable

var PhoneNumberValidation = Class.create();
PhoneNumberValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    checkPhoneNumber: function(phoneNumber) {
        var gr = new GlideRecord('cmdb_ci_comm');
        gr.addQuery('phone_number', this.getParameter('sysparm_phoneNumber'));
        gr.addNotNullQuery('assigned_to');
        gr.query();
        if (gr.next()) {
            return 'true';
        }
        return 'false';
    },

    type: 'PhoneNumberValidation'
});

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Juhi Poddar
Kilo Patron

Hello @MK-p 

You can meet this requirement by writing an onChange client script on Catalog with field phone number.

onChange catalog Client script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return; // Skip validation during form load or when the field is cleared.
    }

    // Perform a GlideAjax call to validate the phone number
    var ga = new GlideAjax('CheckPhoneNumber'); // Replace with your Script Include name.
    ga.addParam('sysparm_name', 'validatePhoneNumber');
    ga.addParam('sysparm_phone', newValue);
    ga.getXMLAnswer(function(response) {
        if (response === 'exists') {
            alert('This phone number already exists and is assigned to someone.');
            g_form.addErrorMessage('This phone number is already in use.');
            g_form.setValue('phone_number', ''); // Clear the field if validation fails.
        }
    });
}

Here 'CheckPhoneNumber' is the script include name.

'validatePhoneNumber' is the function name in script include.

Script include function:

validatePhoneNumber: function() {
        var phoneNumber = this.getParameter('sysparm_phone');
        var gr = new GlideRecord('cmdb_ci_comm'); // Communication table.
        gr.addQuery('phone_number', phoneNumber); // Replace 'phone_number' with the actual field name.
        gr.addQuery('assigned_to', '!=', ''); // Check if assigned_to is not empty.
        gr.query();

        if (gr.next()) {
            return 'exists';
        }
        return 'not_exists';
    },

Note: script include should be client callable.

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

Runjay Patel
Giga Sage

Hi @MK-p ,

 

Follow below steps to perform the validation.

  1. Create onChange catalog client script and check the checkbox for tax.
  2. RunjayPatel_0-1735548968442.png

     

  3. Use below script in client script.

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }


    var ga = new GlideAjax('MobileInventory');
    ga.addParam('sysparm_name', 'checkNumber');
    ga.addParam('sysparm_number', newValue);
    ga.getXML(ValidateNumber);

    function ValidateNumber(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");

        if (answer == 'true') {
            alert('Mobile number already exist');
            g_form.clearValue('mobile_number');

        }
    }
    //Type appropriate comment here, and begin script below

}

 

 

  • Now create a script include and make that client callable, Use below script in it.

 

 

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

    checkNumber: function() {
        var flag = false;
        var number = this.getParameter('sysparm_number');
        var gr = new GlideRecord('cmdb_ci_comm');
        gr.addEncodedQuery('assigned_toISNOTEMPTY^phone_number=' + number);
		gr.query();
        if (gr.next())
            flag = true;

        return flag;
    },



    type: 'MobileInventory'
});

 

 

 

I have check this in my pdi, it working as expected.

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

In this video i have explained about Web service integration in ServiceNow like how it works, how we can configure it, what are the prerequisite and many more. I have covered below topics in this video. 1. understand Web Service. Like when and how we will use it. 2. Talked about Inbound and ...