onChange Client Script to run only when certain variables have been populated
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2019 08:57 AM
Hi,
I am trying to get this working on a catalog client script so it works via the Service Portal. I have the current onChange Client Script which calls a script include to pull data back to another variable (ci_test) which is working great. This is how it works:
You select something from a variable called amend_ci and the client script kicks in and populates data back to the variable ci_test. Here is the Client Script:
onChange
Variable: amend_ci
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('getRelAppParent') ;
ga.addParam('sysparm_name','getApp');
ga.addParam('sysparm_app',newValue);
ga.getXML(hello);
function hello ()
{
var answer = ga.getAnswer();
//alert(answer);
g_form.setValue('ci_test',answer); //network_path is your variable where you want to store path
}
}
Is there something I can add to this script to only run the GlideAjax when another variable on the form is populated (rel_test) this is what I was trying to no avail 😞
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if(g_form.getValue('rel_test', 'Applicative Flow To (parent)')){
var ga = new GlideAjax('getRelAppParent') ;
ga.addParam('sysparm_name','getApp');
ga.addParam('sysparm_app',newValue);
ga.getXML(hello);
}
function hello ()
{
var answer = ga.getAnswer();
//alert(answer);
g_form.setValue('ci_test',answer); //network_path is your variable where you want to store path
}
}
basically I don't want the Client Script to run unless the amend_ci is populated and rel_test is populated with a particular value.
Any assistance would be most appreciated

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2019 09:11 AM
Hello,
I have added a line in your code. Please check of this is working
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if(g_form.getValue('rel_test')!=''){ // or you can use undefined
var ga = new GlideAjax('getRelAppParent') ;
ga.addParam('sysparm_name','getApp');
ga.addParam('sysparm_app',newValue);
ga.getXML(hello);
function hello ()
{
var answer = ga.getAnswer();
//alert(answer);
g_form.setValue('ci_test',answer); //network_path is your variable where you want to store path
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2019 09:27 AM
Hi,
Thank you for getting back me I really appreciate it. I tested with your code line change and this did not work. Let me demonstrate visually what I am after. This is how things work right now with the following Client Script:
On the form when I change 'Which Component Ci are you amending relationship?' the Client Script Runs and 'Component Ci Test' is populated:
What I would like to happen is the following:
You select something from 'Which Component Ci are you amending relationship?' and then you select 'Applicative Flow To (Parent)' from 'Rel Test' then and only then will it run that particular Client Script when 'Applicative Flow To (Parent)' has been selected along with something in the 'Which Component Ci are you amending relationship?' variable. So it would end up looking like this:
I hope that makes sense?
The Client Script is configured to run onChange of the 'Which Component Ci are you amending relationship?' however I don't want it to run unless 'Applicative Flow To (Parent)' has been selected in the rel_test variable.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2019 09:31 AM
Just to confirm Which Component Ci are you amending relationship? = amend_ci
Thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2019 09:52 AM
Update the IF condition as below
if(g_form.getValue('rel_test') == 'Applicative Flow To (parent)'){
Also, you need to write the onChange client script on rel_test variable as well..