Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Fetch state value from change request table to catalog item

Shalika
Tera Expert

I have a catalog item with 2 fields 'change request ID' and 'state of change request' . I have to fetch the state of change request based on change request ID. I have written onChange client script for this but it is not working properly.

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}


var usr = g_form.getReference('change_request_id', callBack);

}

function callBack(usr)
{

g_form.setValue('state_of_change_request', usr.state);

}

It is fetching the value as follow -

Shalika_0-1677062408483.png

In the choice table for state field the value is set as 

Shalika_1-1677062597087.png

It is fetching the value, but I want the label value to be populated on the form. How can I do this?

 

1 ACCEPTED SOLUTION

Hi Shalika,

Check below code-for glideAjax

This is code for client script-

var ga = new GlideAjax('DataHelperAJAX');

ga.addParam('sysparm_name','getState'); getState

ga.addParam('sysparm_if','change_id');

ga.getXML(populateCampus);

function populateCampus(response) {

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

g_form.setValue('campus',answer);

}

}

 

//--you have to create script include and function with name getState

//use this id in script include ,glide to change table ,get state value as-

write this code in script include function-

var id = this.getParameter('change_id');

var gr = new GlideRecord('change_request');

gr.addQuery('sys_id',id);

gr.query();

if(gr.next()){

var state =gr.getDisplayValue('state');

}

return state;

Thanks,

Manjusha 

 

View solution in original post

8 REPLIES 8

Shamma Negi
Kilo Sage
Kilo Sage

GlideReference will not work for getting the displayvalue. You need to use GlideAjax and get the state display value by state.getDisplayValue(); function.

 

Hope this helps.

 

Regards,

Shamma

Regards,Shamma Negi

how to write GlideAjax for this

Hi Shalika,

Check below code-for glideAjax

This is code for client script-

var ga = new GlideAjax('DataHelperAJAX');

ga.addParam('sysparm_name','getState'); getState

ga.addParam('sysparm_if','change_id');

ga.getXML(populateCampus);

function populateCampus(response) {

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

g_form.setValue('campus',answer);

}

}

 

//--you have to create script include and function with name getState

//use this id in script include ,glide to change table ,get state value as-

write this code in script include function-

var id = this.getParameter('change_id');

var gr = new GlideRecord('change_request');

gr.addQuery('sys_id',id);

gr.query();

if(gr.next()){

var state =gr.getDisplayValue('state');

}

return state;

Thanks,

Manjusha 

 

Hi Shilika,

in above code just update below line -

ga.addParam('sysparm_id',g_form.getValue('change_id')); -change_id is name of change id field present on catalog item ,if it is different just update it 

var id = this.getParameter('sysparm_id');

Thanks,

Manjusha Bangale