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

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

 

Actually i tried the script include already but this code works very well.

 

Thank you i can now able to fetch the data.

 

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

@Jeck Manalo 

Thank you for marking my response as helpful.

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