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.

How to make OnChange function work for catalog client script?

User205031
Tera Contributor

Hi All, I have a catalog item with a drop down field 'REGION' and reference field 'Property Name' which references 'cmn_location' table and populates the name field of the MARSHA.

 

So if user doesn't select any value from 'REGION' field , the region present in 'Property Name' of MARSHA will populate in the 'REGION' field.

 

I have written a client script and script include to populate Region of MARSHA in the 'REGION' field if user doesn't select any value from 'REGION' field and it is working fine.

 

Client Script:

function onChange(control, oldValue, newValue, isLoading) {

if (isLoading || newValue == '') {

return;

}

if (g_form.getValue('REGION') == '') {

var a = g_form.getValue('switch_property');

alert(a);

var b = new GlideAjax('PopulateRegion');

b.addParam('sysparm_name', 'getRegion');

b.addParam('sysparm_marsha', a);

b.getXML(getRegion);

 

function getRegion(serverResponse) {

var cc = serverResponse.responseXML.documentElement.getAttribute("answer");

g_form.setValue('REGION', cc);

}

} //Type appropriate comment here, and begin script below }

 

Script Include:

var PopulateRegion = Class.create(); PopulateRegion.prototype = Object.extendsObject(AbstractAjaxProcessor, { getRegion: function () {

var a = this.getParameter('sysparm_marsha');

gs.info('SS2' + a);

var gr = new GlideRecord('cmn_location');

gr.addQuery('sys_id',a);

gr.query();

if (gr.next()) {

var mreg = gr.u_region;

gs.info('SS1' + mreg);

return mreg; }

else {

return '';

}

},

type: 'PopulateRegion'

});

 

But the issue is if the user again changes a Property Name, it is not changing the value of Region field.

How to fix this?

6 REPLIES 6

Nishant8
Giga Sage
Giga Sage

Hello @User205031 , can you try as following and share the outcome:

 

1. create an additional variable of type 'Single line text' and mark as hidden

2. now populate this field with the same value as region something like below:   

function getRegion(serverResponse) {

var cc = serverResponse.responseXML.documentElement.getAttribute("answer");

g_form.setValue('REGION', cc);

g_form.setValue('REGION_hidden', cc); // this is same variable created in #1

}

3. Change if condition of client script as follows: 

if (g_form.getValue('region') == '' || g_form.getValue('region') == g_form.getValue('REGION_hidden'))
 
Regards,
Nishant

Hello @User205031 , hope, you are doing well. wanted to ask whether shared suggestions helped you resolve your issue?

 

Regards,

Nishant