Script Include Glide Ajax & Catalog Client Script help

SnowDevOps
Giga Guru

Hello Team

I want to query the custom table to find out if the user has the fax number or not. If the user has fax number, it alert to display the fax number. If the user doesn't have a fax number, it alerts that you don't have a fax number. I did not get any error code when i refresh the page. I got the alert for "you don't have a fax number". But it's saying that to the users who have the fax number. I'm not sure if the query is running or not. Need some help. 
Thanks 

Script Include:

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

    checkFaxNumber: function() {
        var table = this.getParameter('sysparm_table');
        var query = this.getParameter('sysparm_query');
        var return_type = (this.getParameter('sysparm_return_type' || 'value')).toString();

        //Query the table 
        var gl = new GlideRecord(table);
        gl.addEncodedQuery(query);
        gl.query();
		

        //Collect the fax numbers 
        var result = [];
        while (gl.next()) {
            var val = (return_type === 'display_value');
            gl.getDisplayValue('fax_number');
            gl.getValue('fax_number');
			
        }

        return JSON.stringify(result);


    },
    type: 'SNO_OSS_RecordRetrievalAjaxUtils'
});

Client Script

function onLoad() {

    //Get the current value
    var existingFaxNumber = g_form.getValue('fax_number');
    var currentUserSysId = g_user.userID; //sys_id of logged-in user

    //Build the Ajax Call
    var ga = new GlideAjax('SNW_ETS_RecordRetrievalAjaxUtils');
    ga.addParam('sysparm_name', 'checkFaxNumber');
    ga.addParam('sysparm_table', 'x_figure_ets_fax_number');
    ga.addParam('sysparm_query', 'user=' + currentUserSysId);
    ga.addParam('sysparm_return_type', 'display_value');
    ga.getXML(AJAXcallback);

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

        var faxList = [];
	
			faxList = JSON.parse(answer);
	
        if (Array.isArray(faxList) && faxList > 0) {
            var userFaxNum = faxList[0];
            alert('You already have a fax number. This is your fax number: ' + userFaxNum);
        } else {
            alert("You don't have a fax Number");
        }
    }

}
3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

As posted, the script is most certainly not running since the name used in the GlideAjax call does not match the name of the Script Include posted.  You can troubleshoot this yourself by adding some gs.info or gs.addInfoMessage lines to the Script Include to confirm it is running, and to see how far it gets.  On first glance I'm not really sure what you're trying to do with the var val line, but that's not it.  Guessing at what you really intended, the Script Include should look more like this:

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

	checkFaxNumber: function() {
        var table = this.getParameter('sysparm_table');
        var query = this.getParameter('sysparm_query');
        var return_type = (this.getParameter('sysparm_return_type' || 'value')).toString();

        //Query the table 
        var gl = new GlideRecord(table);
        gl.addEncodedQuery(query);
        gl.query();

        //Collect the fax numbers 
        var result = [];
        while (gl.next()) {
            if ( return_type === 'display_value') {
                result.push(gl.getDisplayValue('fax_number'));
            } else {            
                result.push(gl.getValue('fax_number'));
        }

        return result.join(',');

    },

    type: 'SNW_ETS_RecordRetrievalAjaxUtils'
});
function onLoad() {

    var currentUserSysId = g_user.userID; //sys_id of logged-in user

    //Build the Ajax Call
    var ga = new GlideAjax('SNW_ETS_RecordRetrievalAjaxUtils');
    ga.addParam('sysparm_name', 'checkFaxNumber');
    ga.addParam('sysparm_table', 'x_figure_ets_fax_number');
    ga.addParam('sysparm_query', 'user=' + currentUserSysId);
    ga.addParam('sysparm_return_type', 'display_value');
    ga.getXML(AJAXcallback);

    function AJAXcallback(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer != '') {        
            var userFaxNum = answer.split(',')[0];
            alert('You already have a fax number. This is your fax number: ' + userFaxNum);
        } else {
            alert("You don't have a fax Number");
        }
    }

}

 

rafaelramos0
Tera Expert

Hi @SnowDevOps !

I did review your scripts and there are some points to be observed.
#1 - Looks like the script include name is not okay in your client script
#2 - Script include function was not passing the value to result variable.

See my code:
Script Include name: SNW_ETS_RecordRetrievalAjaxUtils
Client Callable: true

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

	checkFaxNumber: function() {
        var table = this.getParameter('sysparm_table');
        var query = this.getParameter('sysparm_query');
        // var return_type = (this.getParameter('sysparm_return_type' || 'value')).toString();

        //Query the table 
        var gl = new GlideRecord(table);
        gl.addEncodedQuery(query);
        gl.query();
		

        //Collect the fax numbers 
        var result = [];
        while (gl.next()) {
            // var val = (return_type === 'display_value');
            // gl.getDisplayValue('mobile_phone');
            
			result.push(gl.getValue('mobile_phone'));
        }

        return JSON.stringify(result);

    },

    type: 'SNW_ETS_RecordRetrievalAjaxUtils'
});

 

Client Script

function onLoad() {

    //Get the current value
    var existingFaxNumber = g_form.getValue('fax_number');
    var currentUserSysId = g_user.userID; //sys_id of logged-in user

    //Build the Ajax Call
    var ga = new GlideAjax('SNW_ETS_RecordRetrievalAjaxUtils');
    ga.addParam('sysparm_name', 'checkFaxNumber');
    ga.addParam('sysparm_table', 'sys_user');
    ga.addParam('sysparm_query', 'sys_id=' + currentUserSysId);
    // ga.addParam('sysparm_return_type', 'display_value');
    ga.getXML(AJAXcallback);

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

        var faxList = [];
	
			faxList = JSON.parse(answer);
	
        if (Array.isArray(faxList) && faxList > 0) {
            var userFaxNum = faxList[0];
            alert('You already have a fax number. This is your fax number: ' + userFaxNum);
        } else {
            alert("You don't have a fax Number");
        }
    }

}




Mark helpful if makes sense.  🙂

 

 

Mohammed8
Giga Sage

@SnowDevOps 

@Brad Bowman ,  @rafaelramos0  and  have pointed out exact issues.

Also I see result declared but not used, maybe you missed it out.

 

Mohammed8_0-1766523432973.png

 

 Thanks and Regards,

Mohammed Zakir