How to make OnChange function work for catalog client script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2025 02:15 AM - edited 01-30-2025 02:17 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2025 02:24 AM
Hi @User205031 ,
Can you write one more OnChange client script to clear the value, and the user needs to select again.
Regards,
Najmuddin.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2025 02:32 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2025 02:32 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2025 02:38 AM
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