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

5 REPLIES 5

MK-p
Tera Contributor

Thanks @Ankur Bawiskar @Juhi Poddar @Runjay Patel for the help.