On-change script dotwalking

Jeck Manalo
Tera Guru

Hello Everyone,

 

I am having an issue with regards of fetching the data from variables in sc_req_item table.

 

So i have a new catalog and one of the variable is a reference table into (sc_req_item)

So the idea is i need to get the variable data in that reference table, I can see that i can able to get some data in req item but for variables it shows undefined.

 

also addinfo message  and g_form.setValue is only working to tech view and not in sp portal view. i add it in info message just to see if i get a data but i will input it in the variable using g_form.setValue but again its only working when i was using in technical view not in sp portal.

 

 

I do try to transform this code in script include / onchange script but have same result. Any idea ?

 

 

JeckManalo_1-1746148964342.png

 

function onChange(control, oldValue, newValue, isLoading) {
 // Type appropriate comment here, and begin script below
 if (isLoading) {
 return;
 }

 
 var reqItem = new GlideRecord('sc_req_item');
 reqItem.addQuery('sys_id', newValue);
 reqItem.query();

 if (reqItem.next()) {
 var catsysid = reqItem.cat_item; // catalog sysid
    g_form.addInfoMessage('Catalog Item Name: ' + catsysid);


 var itemcat = new GlideRecord('sc_cat_item');
 itemcat.addQuery('sys_id', catsysid);
 itemcat.query();
 // g_form.addInfoMessage('Catalog Sysid: ' + catsysid);

 if (itemcat.next())
 {
 var cname = itemcat.name;
 g_form.addInfoMessage('Catalog Item Name: ' + cname);
//  g_form.setValue('userid_of_terminated_user', cname);
 
 }

 if (cname == 'TEST2') {
 var useridwd = reqItem.variables.test2;
 g_form.addInfoMessage('User ID WD: ' + useridwd);
 }

 if (cname == 'TEST2)') {
 var userid = reqItem.variables.test2;
 g_form.addInfoMessage('User ID: ' + userid);
 }
 }
}
2 ACCEPTED SOLUTIONS

Chaitanya ILCR
Kilo Patron

Hi @Jeck Manalo ,

 

it's not a good practice to use GlideRecord directly in client script

 

use GlideAjax instead

script include

var GetInforCatalog = Class.create();
GetInforCatalog.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getDetails: function() {
        var res = {
            catName: '',
            userid: ''
        };
        var reqItem = new GlideRecord('sc_req_item');
        if (reqItem.get(this.getParameter('reqsysid'))) {
            res.catName = reqItem.cat_item.getDisplayValue();
            res.userid = reqItem.variables.test2 ? reqItem.variables.test2 : '';
			// extend your logic here
        }
        return JSON.stringify(res);

    },
    type: 'GetInforCatalog'
});

 

Client script
assuming this on the onchange of the RITM reference variable

var ga = new GlideAjax('GetInforCatalog');
ga.addParam('sysparm_name', 'getDetails');
ga.addParam('reqsysid', newValue);
ga.getXMLAnswer(function(answer) {
    alert(answer);
    answer = JSON.parse(answer);
    g_form.addInfoMessage('Catalog Item Name: ' + answer.catName);
    g_form.addInfoMessage('User ID WD: ' + answer.userid);
	//add you logic here
})

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@Jeck Manalo 

you cannot dot walk variables.variableName in client side

you should not use GlideRecord in client side as it's not recommended.

You can use onChange + GlideAjax and get the variable values

Something like this which I shared solution but please enhance, you can use normal onchange client script instead of catalog client script, but please enhance

How to access ritm variables (variables used in catalog item) using catalog client script? 

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

5 REPLIES 5

AnveshKumar M
Tera Sage
Tera Sage

Hello @Jeck Manalo 

GlideRecord API at client side has limitations, and it is not recommended to use it in ClientScript due to performance impacts. The alternative is call a ScriptInclude via GlideAjax.

 

In your case, you can try the following approach.

 

1. Script Include: Create a client callable (GlideAjax Enabled) script include like the one below.

AnveshKumarM_0-1746154720508.png

 

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

    getVariables: function() {
		var ritm_sys_id = this.getParameter('sysparm_ritm');
		var resp_data = this._getVariables(ritm_sys_id);
		try{
			return JSON.stringify(resp_data);
		}catch(ex){
			var error_resp = {
				result: false,
				error: ex.toString()
			};

			return JSON.stringify(error_resp);
		}
    },

    _getVariables: function(ritm_sys_id) {
        //Get RITM
        var ritmGr = new GlideRecord('sc_req_item');
        ritmGr.addQuery('sys_id', ritm_sys_id);
        ritmGr.query();
        var resp_data = {};
        if (ritmGr._next()) {
            resp_data['result'] = true;
            resp_data['cat_item_name'] = ritmGr.cat_item.name.getDisplayValue(); //Catalog Item Name
            var cat_item_vars = ritmGr.variables;
            var variables = {};
            for (key in cat_item_vars) {
                variables[key] = cat_item_vars[key].toString();
            }
            resp_data['variables'] = variables;
            return resp_data;
        }
        resp_data['result'] = false;
		resp_data['error'] = 'Request Item not found for: ' + ritm_sys_id;

        return resp_data;
    },

    type: 'CatalogUtils'
});

 

2. Client Script: Change your client script to the one like below.

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

    var cuGa = new GlideAjax('global.CatalogUtils');
    cuGa.addParam('sysparm_name', 'getVariables');
    cuGa.addParam('sysparm_ritm', newValue);
    cuGa.getXMLAnswer(processResponse);

    function processResponse(answer) {
        if (answer) {
            try {
                var data = JSON.parse(answer);
                if (data.result) {
                    var cat_item_name = data.cat_item_name;
                    var variables = data.variables;
                    if (cat_item_name == 'TEST2') {
                        g_form.addInfoMessage('User ID: ' + variables.test2);
                    }
                } else {
                    g_form.addErrorMessage(data.error);
                }
            } catch (ex) {
				g_form.addErrorMessage(ex.toString());
            }
        } else {
            g_form.addErrorMessage("Processing error.");
        }
    }

}

 

 Please mark my answer helpful 👍 and mark the solution as accepted ✔️ if this works for you.

Thanks,
Anvesh