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.

else if condition is not working for the "None" value of variable(Variable type: "select box")

Khaja Shaik
Tera Contributor

Hi All,

 

Hope you are doing well!!

 

I have the below requirement.

 

I have a variable "Access type", variable type is Select box(Include none is true) and choices are 1) Single, 2) Group.

Point 1: If you select the choice as "Single"  a new variable "Single access" is visible, mandatory and add the choices as mentioned in the below code.

Point 2: If you select the choice as "Group" a new variable "Group access" is visible, mandatory and add the choices as mentioned in the below code.

Point 3: If you select the choice as "None" after selecting point 1or point 2, it should clear the point 1 or point 2 value and hide the variables "Single access" or "Group access".

 

I have created below onChange client script for this requirement. The point 1 and point 2 is working fine but point 3(none condition) is not working. Please correct my below code if its wrong.

 

OnChange client script Code:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

//Type appropriate comment here, and begin script below
if (newValue == 'Single') {
g_form.setVisible('single_access ', true);
g_form.setMandatory('group_access', true);

g_form.clearValue('group_access');
g_form.setMandatory('group_access', false);
g_form.setVisible('group_access', false);

 

g_form.addOption('single_access', 'Teams', 'Teams', 1);
g_form.addOption('single_access', 'Email', 'Email', 2);
g_form.addOption('single_access', 'Outlook', 'Outlook', 3);
g_form.addOption('single_access', 'Data', 'Data', 4);

 

} else if (newValue == 'group_access') {
g_form.setVisible('group_access', true);
g_form.setMandatory('group_access', true);

g_form.clearValue('single_access');
g_form.setMandatory('single_access', false);
g_form.setVisible('single_access', false);

 

g_form.addOption('group_access', 'Grp1', 'Grp1', 1);
g_form.addOption('group_access', 'Grp2', 'Grp2', 2);
g_form.addOption('group_access', 'Grp3', 'Grp3', 3);
g_form.addOption('group_access', 'Grp4', 'Grp4', 4);


} else if(newValue == ''{ //new value is 'none' This part is not working

g_form.clearValue('single_access');
g_form.clearValue('group_access');
g_form.setMandatory('group_access', false);
g_form.setMandatory('single_access', false);
g_form.setVisible('group_access', false);
g_form.setVisible('single_access', false);

}

}

 

Thanks! in advance

1 ACCEPTED SOLUTION

Sujatha V M
Kilo Patron
Kilo Patron

@Khaja Shaik  Please modify as below,

You already have newValue == ‘’ and you can declare those within this loop. 

if (isLoading || newValue == '') {
g_form.clearValue('single_access');
g_form.clearValue('group_access');
g_form.setMandatory('group_access', false);
g_form.setMandatory('single_access', false);
g_form.setDisplay('group_access', false);
g_form.setVisible('single_access', false);
}


if (newValue == 'Single') {
g_form.setVisible('single_access ', true);
g_form.setMandatory('group_access', true);

g_form.clearValue('group_access');
g_form.setMandatory('group_access', false);
g_form.setVisible('group_access', false);

 

g_form.addOption('single_access', 'Teams', 'Teams', 1);
g_form.addOption('single_access', 'Email', 'Email', 2);
g_form.addOption('single_access', 'Outlook', 'Outlook', 3);
g_form.addOption('single_access', 'Data', 'Data', 4);

 

} else if (newValue == 'group_access') {
g_form.setVisible('group_access', true);
g_form.setMandatory('group_access', true);

g_form.clearValue('single_access');
g_form.setMandatory('single_access', false);
g_form.setVisible('single_access', false);

 

g_form.addOption('group_access', 'Grp1', 'Grp1', 1);
g_form.addOption('group_access', 'Grp2', 'Grp2', 2);
g_form.addOption('group_access', 'Grp3', 'Grp3', 3);
g_form.addOption('group_access', 'Grp4', 'Grp4', 4);

}

 


If you want it to in your ‘If and Else’ statement, then you have it as below,

if (isLoading) {
return;
}

 

The reason for last condition not working is because it’s getting returned as the syntax is same and  used twice.

Note : setVisible() will create spaces instead using setDisplay().

 

Please mark this as helpful and accept it as a solution if this resolves your query.

Thanks,

Sujatha V.M.

Please mark this as helpful and accept it as a solution if this resolves your query.
Sujatha V.M.

View solution in original post

6 REPLIES 6

Sujatha V M
Kilo Patron
Kilo Patron

@Khaja Shaik  Please modify as below,

You already have newValue == ‘’ and you can declare those within this loop. 

if (isLoading || newValue == '') {
g_form.clearValue('single_access');
g_form.clearValue('group_access');
g_form.setMandatory('group_access', false);
g_form.setMandatory('single_access', false);
g_form.setDisplay('group_access', false);
g_form.setVisible('single_access', false);
}


if (newValue == 'Single') {
g_form.setVisible('single_access ', true);
g_form.setMandatory('group_access', true);

g_form.clearValue('group_access');
g_form.setMandatory('group_access', false);
g_form.setVisible('group_access', false);

 

g_form.addOption('single_access', 'Teams', 'Teams', 1);
g_form.addOption('single_access', 'Email', 'Email', 2);
g_form.addOption('single_access', 'Outlook', 'Outlook', 3);
g_form.addOption('single_access', 'Data', 'Data', 4);

 

} else if (newValue == 'group_access') {
g_form.setVisible('group_access', true);
g_form.setMandatory('group_access', true);

g_form.clearValue('single_access');
g_form.setMandatory('single_access', false);
g_form.setVisible('single_access', false);

 

g_form.addOption('group_access', 'Grp1', 'Grp1', 1);
g_form.addOption('group_access', 'Grp2', 'Grp2', 2);
g_form.addOption('group_access', 'Grp3', 'Grp3', 3);
g_form.addOption('group_access', 'Grp4', 'Grp4', 4);

}

 


If you want it to in your ‘If and Else’ statement, then you have it as below,

if (isLoading) {
return;
}

 

The reason for last condition not working is because it’s getting returned as the syntax is same and  used twice.

Note : setVisible() will create spaces instead using setDisplay().

 

Please mark this as helpful and accept it as a solution if this resolves your query.

Thanks,

Sujatha V.M.

Please mark this as helpful and accept it as a solution if this resolves your query.
Sujatha V.M.

Khaja Shaik
Tera Contributor

Hi @Sujatha V M

 

Thank you very much for your quick response. Now it is working fine. 

Thank you once again! 

Hi @Sujatha V M

Please help me with the below code. Here we have to show the below variables(Mentioned in the code) 6 times and I have created 6 OnChange client scripts as mentioned below, all the client scripts are working fine but 5th client script not working. Please see the below script and correct if I made any mistake.

Thanks!! in advance.

 

This OnChange client script is created on the variable "do_you_need_access_to_other_environments_and_or_technology_4" This is the check box.


if (newValue == 'false') {

g_form.setMandatory('environment_5', false);
g_form.setMandatory('technology_5', false);

g_form.clearValue('environment_5');
g_form.clearValue('technology_5');
g_form.setDisplay('do_you_need_access_to_other_environments_and_or_technology_6', false); //This is the check box
g_form.setDisplay('environment_5', false);
g_form.setDisplay('technology_5', false);
g_form.setDisplay('do_you_need_access_to_other_environments_and_or_technology_6', false); //This is the check box
return;
}

//Type appropriate comment here, and begin script below
if (newValue == 'true') {
g_form.setMandatory('environment_5', true);
g_form.setMandatory('technology_5', true);
g_form.setDisplay('environment_5', true);
g_form.setDisplay('technology_5', true);
g_form.setDisplay('do_you_need_access_to_other_environments_and_or_technology_6', true); //This is the check box
g_form.addOption('environment_5', 'Production', 'Production', 1);
g_form.addOption('environment_5', 'Test', 'Test', 2);
g_form.addOption('environment_5', 'test2', 'test2', 3);
g_form.addOption('environment_5', 'UAT', 'UAT', 4);

g_form.addOption('technology_5', 'tech1', 'tech1', 1);
g_form.addOption('technology_5', 'tech2', 'tech2', 2);

}


}

@Khaja Shaik  It seems the "Return" value is stopping the execution from not setting it. Can you try the below,

 

if (isLoading ) {

return;
}

 

if(newValue == 'true'{

g_form.setMandatory('environment_5', true);
g_form.setMandatory('technology_5', true);
g_form.setDisplay('environment_5', true);
g_form.setDisplay('technology_5', true);
g_form.setDisplay('do_you_need_access_to_other_environments_and_or_technology_6', true); //This is the check box
g_form.addOption('environment_5', 'Production', 'Production', 1);
g_form.addOption('environment_5', 'Test', 'Test', 2);
g_form.addOption('environment_5', 'test2', 'test2', 3);
g_form.addOption('environment_5', 'UAT', 'UAT', 4);

g_form.addOption('technology_5', 'tech1', 'tech1', 1);
g_form.addOption('technology_5', 'tech2', 'tech2', 2);

}

else {

g_form.setMandatory('environment_5', false);
g_form.setMandatory('technology_5', false);

g_form.clearValue('environment_5');
g_form.clearValue('technology_5');
g_form.setDisplay('do_you_need_access_to_other_environments_and_or_technology_6', false); //This is the check box
g_form.setDisplay('environment_5', false);
g_form.setDisplay('technology_5', false);
g_form.setDisplay('do_you_need_access_to_other_environments_and_or_technology_6', false); //This is the check box

}

 

Do try and let me know.

 

Please mark this as helpful and accept it as a solution if this resolves your query.

Thanks,

Sujatha V.M.

Please mark this as helpful and accept it as a solution if this resolves your query.
Sujatha V.M.