can we use GlideAjax 2 times in the same client script?

Bhavana Reddy
Mega Guru

Hello Guys,

We have a onchange client script written to automatically set the values of the catalog item onchange of "Quote" field and now we introduced the MRV's and we also need to automatically populate the values in the MRV based on the "Quote" value and i am thinking to create a seperate function in the script include and can we use 2 times the GlideAjax and call our seperate function and fill the MRV's data?

 

Please suggest me!!

1 ACCEPTED SOLUTION

So, in the script include, change to as follows. ServiceNow defaults field names to all lowercase.

obj["vendor"] = equipment.u_vendor+'';
obj["eqptype"] = equipment.u_equipment_type_model_no+'';
obj["quantity"] = equipment.u_quantity+'';
obj["racunt"] = equipment.u_rack_units+'';

View solution in original post

18 REPLIES 18

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi  Bhavana,

I've tested using 2 ajax calls in 1 onChange client and found not problem if the calls are independent of each other.

> i am thinking to create a seperate function in the script include

If both ajax functions are going to be in the same script include class, it would be easier to create a function that combines both ajax calls into one.

In the script include, concatenate the call to 2 functions.

e.g.

	result['name'] = this.getUserName(user_id);  // call to function to get user name
	result['role'] = this.getRoleInfo(user_id);  // call to function to get user's roles
        return JSON.stringify(result);

Then in the client script, just fetch the result after parsing the returned json object.

e.g.

            var answer = response.responseXML.documentElement.getAttribute("answer");
            var json = JSON.parse(answer);
            g_form.setValue('name', json.name); // get result from calling getUserName function in script include
            var role = json.role;  // get result from calling getRoleInfo in script include

Hi Hitoshi,

 

Yes i got what you are saying, My problem here is 

1)In the first function we are just storing everything in the Results {} object. 

2) In the 2nd function to fetch the values from the Custom table to set it to the MRV's ( i am pusing objects into the array )

I am using the below script

find_real_file.png

script include

find_real_file.png

In this case i cant write the script as suggested by you right? since for MRV's we need to use the for loop and fetch the values in the objects inside the array, Please guide me 

 

Thanks,

Bhavana

Hitoshi Ozawa
Giga Sage
Giga Sage

Fixed the code in question to only make 1 ajax call. Script Include will return a json object. The first element will be 'customerOrder' will values returned from getCustomerOrder() method and the second element will be an nested object returned from getMRVData() methods.

The client script will call getFormData() method that will in turn call getCustomerOrder() and getMRVData() functions. getCustomerOrder() and getMRVData() functions will each return an object and getFormData will stringify() results into 1 json string.

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ID = g_form.getValue('customer_order_no');
    var ga = new GlideAjax('ACUXValues');
    ga.addParam('sysparm_name', 'getFormData');
    ga.addParam('sysparm_custOrd', ID);
    ga.getXML(populateDetails);

    function populateDetails(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer) {
            var returndata = JSON.parse(answer);
            var customerOrder = returndata.customerOrder;
            var mrvData = returndata.mrvData;
            if (g_form.getValue('type_of_change') == 'Technical Data Change') {
                g_form.setValue("opportunity_id", customerOrder.opportunity_no);
                g_form.setValue("svo", customerOrder.svo_no);
            }
            if (customerOrder.Service_id != '') {
                g_form.setValue("service_id", customerOrder.Service_id);
            }
            if (customerOrder.CSE_notes != '') {
                g_form.setValue("cse_notes", customerOrder.CSE_notes);
            }
            g_form.setValue("is_pm_required", customerOrder.is_pm_required);
            g_form.setValue("modem_type", customerOrder.Modem_Type);
            if (customerOrder.Customer_Location != '') {
                g_form.setValue("customer_location", customerOrder.Customer_Location);
            }
            if (customerOrder.colo == 'Yes') {
                g_form.setValue("collocation", 'yes');
            }
            if (customerOrder.colo == 'No') {
                g_form.setValue("collocation", 'no');
            }
			// logic to be implemented for filling MRV's from Custom table - use variable "mrvData"
        }
    }
}

Script Include:

var ACUXValues = Class.create();
ACUXValues.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getFormData: function() {
        var custOrd = this.getParameter('sysparm_custOrd');
        var result = {};
        result['customerOrder'] = this.getCustomerOrder(custOrd);
        result['mrvData'] = this.getMRVData(custOrd);
        return JSON.stringify(result);
    },
    getCustomerOrder: function(custOrd) {

    },
    getMRVData: function(custOrd) {
        var gr = new GlideRecord('sn_ind_tmt_orm_order');
        gr.addQuery('sys_id', custOrder);
        gr.query();
        if (gr.next()) {
            /// ...
            while (equipment.next()) {
				// ...
                results.push(obj);
            }
            return results;
        }
    },
    type: 'ACUXValues'
});

Hitoshi Ozawa
Giga Sage
Giga Sage

Sample working script to fill user name field and mrv fields with user roles with 1 ajax call.

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ajax = new GlideAjax('GetUserInformation');
    ajax.addParam('sysparm_name', 'getUserInfo');
    ajax.addParam('sysparm_user_id', newValue);
    ajax.getXML(_displayUserInfo);

    function _displayUserInfo(response) {
            var answer = response.responseXML.documentElement.getAttribute("answer");
            var json = JSON.parse(answer);
            g_form.setValue('name', json.name);
            g_form.setValue('user_role', JSON.stringify(json.role));
    }
}

Script Include:

var GetUserInformation = Class.create();
GetUserInformation.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getUserInfo: function() {
        var user_id = this.getParameter('sysparm_user_id');
        var result = {};
        result['name'] = this.getUserName(user_id);
        result['role'] = this.getRoleInfo(user_id);
        return JSON.stringify(result);
    },

    getUserName: function(user_id) {
        var grUser = new GlideRecord('sys_user');
        if (grUser.get(user_id)) {
            return grUser.user_name.toString();
        }
        return;
    },

    getRoleInfo: function(user_id) {
        var grUserRole = new GlideRecord('sys_user_has_role');
        grUserRole.addQuery('user', user_id);
        grUserRole.query();
        var result = [];
        while (grUserRole.next()) {
            var roleInfo = {};
            roleInfo['role'] = grUserRole.role.name.toString();
            roleInfo['state'] = grUserRole.state.toString();
            result.push(roleInfo);
        }
        return result;
    },

    type: 'GetUserInformation'
});

Execution result. When user is selected in field "User", fields "Name" and mvr fields are populated with values.

find_real_file.png

Hi Hitoshi,

I tried this script and it doesnt work for me , while trying to set the Multiple columns using the for loop and it returns the indivisual words and shows empty rows in the MRV( My MRV has 4 columns)

 

Thanks