onChange Client Script to run only when certain variables have been populated

danielbartholom
Mega Expert

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 

  

8 REPLIES 8

Jagadeesh R1
Tera Expert

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


}

}

}

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:

find_real_file.png

 

On the form when I change 'Which Component Ci are you amending relationship?' the Client Script Runs and 'Component Ci Test' is populated:

find_real_file.png

 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:

find_real_file.png

 

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

Just to confirm Which Component Ci are you amending relationship? = amend_ci

 

Thanks

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..