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

Khaja Shaik
Tera Contributor

Hi @Sujatha V M,

 

I have tried what you suggested, Its not working.

Here I have observed with my code is, if I change the order from 100 to 10 the variable "do_you_need_access_to_other_environments_and_or_technology_6" is Hiding but remaining two variables "technology_5" and "environment_5" not Hiding.

@Khaja Shaik Can you tell me what's not working here? Is it not hiding the fields ? Can you share the screenshots?

 

If you have copied the code below, I had missed a ")", in that if(newValue == 'true' sentence. 

 

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.