How to use a Catalog Client Script to get and compare a users extension

Travis Michigan
Mega Sage

I'm trying to have a catalog client script that works onChange to gather information on the user and compare their entry in the monitored field newValue and make sure that it doesn't contain their extension.  However I am going in circles, any tips?  Here are parts of the Catalog Client Script:

    function setPhone(user, newValue) {
		var ga = new GlideAjax('getExt');
		ga.addParam('sysparm_name', "getPhoneExtension");
		ga.addParam('sysparam_user', user);
		ga.addParam('sysparam_pin', newValue);
		return (ga.getXMLAnswer(_handleResponse));
		// alert(g_scratchpad.extension);
		// 	return g_scratchpad.extension;
    }

	function _handleResponse(response) {
		alert(response);
		g_scratchpad.extension = false;
	}

Then this is the script include

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

    getPhoneExtension: function() {
        var userSys = this.getParameter('sysparam_user');
        var pin = this.getParameter('sysparam_pin');
        gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id', userSys);
        gr.query();
        if (gr.next()) {
            var businessPhone = gr.phone.split('x');
            alert(businessPhone + 'busPhone');
            var phone = businessPhone[1];
            if (phone.indexOf(pin)) {
                return 'true';
            } else {
                return 'false';
            }
        }
    },


    type: 'getExt'
});

But it just returns null.  Any guidance on what I'm missing?  Or if there is an easier way to accomplish this?  Thanks in advance.

Then this is the 

3 REPLIES 3

Maddysunil
Kilo Sage

@Travis Michigan 

Your setPhone function in the catalog client script is not handling the response properly. It should wait for the response from the GlideAjax call before proceeding. You should pass a callback function to handle the response

 

function setPhone(user, newValue) {
    var ga = new GlideAjax('getExt');
    ga.addParam('sysparm_name', "getPhoneExtension");
    ga.addParam('sysparam_user', user);
    ga.addParam('sysparam_pin', newValue);
    ga.getXMLAnswer(_handleResponse); // Pass _handleResponse as a callback
}

function _handleResponse(response) {
    alert(response);
    // Now you can handle the response appropriately
    // For example, you can set g_scratchpad.extension based on the response
    g_scratchpad.extension = (response === 'true');
}

 

In Script Include 

There are a couple of issues in your getPhoneExtension function in the script include:

  • The logic for checking the pin in the phone extension seems incorrect. You're trying to check if the pin exists in the phone extension, but the logic needs adjustment.
  • You should return the response using this.answer() method instead of directly returning the value.

 

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

    getPhoneExtension: function() {
        var userSys = this.getParameter('sysparam_user');
        var pin = this.getParameter('sysparam_pin');
        var gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id', userSys);
        gr.query();
        if (gr.next()) {
            var businessPhone = gr.phone;
            if (businessPhone.includes('x')) {
                var phoneParts = businessPhone.split('x');
                var phoneExtension = phoneParts[1].trim(); // Trim to remove any leading/trailing spaces
                if (phoneExtension === pin) {
                    return this.answer('true'); // Return true if pin matches extension
                }
            }
        }
        return this.answer('false'); // Return false if pin doesn't match extension or if no extension found
    },

    type: 'getExt'
});

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

Seraj
Tera Guru

Hi @Travis Michigan 

Please can you explain your requirement bit more.

 

Best Regards

Seraj

I'm trying to check and see if the newValue contains part of the extension, as the extension (the portion after the x in the phone).  So I'm trying to get it to get the phone off of the sys_user table, split the phone into 2 parts, gather the second portion which will be the extension.  Then compare the newValue to see if it contains the extension within it.

 

If the user had an extension of 1234349 and the users extension was say 2343 then it should reject it.  I probably should have included what is calling setPhone too.  


	var user = g_form.getValue('requested_for');
    if (setPhone(user, newValue)) {
        g_form.showFieldMsg('new_pin', 'Invalid PIN, cannot contain extension/mailbox', 'error', true);
        g_scratchpad.isFormValid = false;
    }