Client Script nested IF statement weirdness

PLakin1
Tera Guru

Can anyone explain to me why this worked:

 

 

 

function onLoad()  
{  
	var citem = g_form.getValue('cat_item');
	var cstate = g_form.getValue('state');
	if(citem == 'c9ca72c5db52f5500a5df6a4e2961944' && cstate == '1' || cstate == '2') {
		//alert(cstate);
			g_form.setVariablesReadOnly(false);
		//}
		}
	else {
			g_form.setVariablesReadOnly(true);
		}
} 

 

 

 

but this didn't: 

 

 

function onLoad()  
{  
	var citem = g_form.getValue('cat_item');
	var cstate = g_form.getValue('state');
	if(citem == 'c9ca72c5db52f5500a5df6a4e2961944') {
          if(cstate == '1' || cstate == '2') {
		//alert(cstate);
			g_form.setVariablesReadOnly(false);
		//}
		}}
	else {
			g_form.setVariablesReadOnly(true);
		}
} 

 

 

 

You could argue the first is cleaner, but I think the second is easier to follow. In any case, I banged my head against the bottom one without any understanding why it failed before trying the top one. The only thing that made any sense was maybe the variables weren't accessible even though I created them before any of the statements.

Any help is appreciated. Thanks!

2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@PLakin1 

in the 1st script you are doing AND between citem and cstate value 1

but in the 2nd one you are checking if cstate is 1 or 2

So both the scripts have different logic

Now it's upto your requirement what customer needs

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

@PLakin1 

To make your second script work similar to the first one you can try doing the following modifications to your second script.

 

function onLoad()  {  

 var citem = g_form.getValue('cat_item');

 var cstate = g_form.getValue('state');

 if(citem == 'c9ca72c5db52f5500a5df6a4e2961944') {

      if(cstate == '1' || cstate == '2') {

         g_form.setVariablesReadOnly(false);

      } else {

         g_form.setVariablesReadOnly(true);

      }

   } else {

      g_form.setVariablesReadOnly(true);

  }

}

 

Please mark my answer helpful and accept as solution if it helped 👍

Thanks,
Anvesh

View solution in original post

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

@PLakin1 

in the 1st script you are doing AND between citem and cstate value 1

but in the 2nd one you are checking if cstate is 1 or 2

So both the scripts have different logic

Now it's upto your requirement what customer needs

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

I'm checking for both in both scripts. I just am checking for both at the same time in the first statement. They both check for the item and the state.

The second statement causes the RITM to always have the variables editable, even when the state isn't 1 or 2, which shouldn't happen based upon the logic. The first statement works correctly, only making the variables editable when the RITM is for the correct catalog item and the correct state.

@PLakin1 

To make your second script work similar to the first one you can try doing the following modifications to your second script.

 

function onLoad()  {  

 var citem = g_form.getValue('cat_item');

 var cstate = g_form.getValue('state');

 if(citem == 'c9ca72c5db52f5500a5df6a4e2961944') {

      if(cstate == '1' || cstate == '2') {

         g_form.setVariablesReadOnly(false);

      } else {

         g_form.setVariablesReadOnly(true);

      }

   } else {

      g_form.setVariablesReadOnly(true);

  }

}

 

Please mark my answer helpful and accept as solution if it helped 👍

Thanks,
Anvesh

Well, you caught it before I did, sir. You're right, because of the first IF statement it won't satisfy the bottom ELSE. Another one I should have seen sooner.

 

Thank you!