The CreatorCon Call for Content is officially open! Get started here.

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

Najmuddin Mohd
Mega Sage

Hi @User205031 ,
Can you write one more OnChange client script to clear the value, and the user needs to select again.

Regards,
Najmuddin.

Hi Najmuddin,

 

In this case, if the user selects any value from 'REGION' and then select 'Property Name', it is clearing out the 'REGION' field which user is selecting at first.

 

My issue is if user doesn't select any value from 'REGION' and selects a 'Property Name' it populates the region in the 'REGION' field and again if user selects a different 'Property Name' it should change the region in 'REGION' field. 

 

Thanks

Ankur Bawiskar
Tera Patron
Tera Patron

@User205031 

you are creating onChange on Region and setting the value again on Region?

Should you not write onChange on that reference field i.e. Property Name?

Write onChange on Property Name and if user selects the location then bring the value and set in Region field

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

Hi Ankur,

 

I have written onChange client script on reference field 'Property Name' .

 

If user doesn't select any value from 'REGION' and selects a 'Property Name' it populates the region in the 'REGION' field and again if user selects a different 'Property Name' it should change the region in 'REGION' field but it is not changing.

 

Thanks