Confusion on using "&&" " || " in onload client script

Ajay37
Tera Contributor

Hi All,

I have 5 state fields on custom table and I have got a requirement like, making the fields read-only for user in 3,4,5 states. I have written onLoad script for this, but I got confused in using the Logical operator. 

Initially I have tried like below:

 if(g_user.hasRole('u_matter_management_user') && (g_form.getValue('state') != '-3' || g_form.getValue('state') != '-2'))

It didn't worked. 

Later I tried like below:

 if(g_user.hasRole('u_matter_management_user') && (g_form.getValue('state') != '-3' && g_form.getValue('state') != '-2'))

It is working. My doubt here is, we are saying either of the states right, so I though to Use OR condition, but didn't work. Could anybody please explain on these operators.

If AND is to be used, I have written a script to make all fields read-only in closed states, using OR condition which is working fine.

function onLoad() {
//Type appropriate comment here, and begin script below
if(!g_user.hasRole("admin") && (g_form.getValue("state")==4 || g_form.getValue("state")==5 || g_form.getValue("state")==6)) {
var fields = g_form.getEditableFields();
for (var x = 0; x < fields.length; x++) {
g_form.setReadOnly(fields[x], true);
}
}
}

Here I have used OR condition only, and it is working fine. But why it is not working in first requirement.

1 ACCEPTED SOLUTION

I know it's little confusing but if you think about it is working as it should.

When you use OR then all tickets will show up because you are saying if the ticket is not in -3 state or -2 state then show.

The other you can look at it like if state is -3 and -2 then show results then it will only show tickets that are not in either state.

Refer to (page 10 and 11)- https://www.servicenow.com/content/dam/servicenow/other-documents/training/ServiceNow-JavaScript-Pri...

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence

View solution in original post

6 REPLIES 6

Mike Patel
Tera Sage

you need below

if(g_user.hasRole('u_matter_management_user') && (g_form.getValue('state') != '-3' && g_form.getValue('state') != '-2'))

Basically your condition is saying state is not -3 and not -2 so that shows you all other state items.

 

When you use != then you have to use && instead of || so you get results without those 2 states

 

Thanks Mike,

Yes I used that, I just confused about using OR and AND. We are saying like either of the state fields, so as per this we need to use OR right, if we use AND it is saying like when state is not both -3 and -2. I am really confused with this concept, could you please explain clearly.

I know it's little confusing but if you think about it is working as it should.

When you use OR then all tickets will show up because you are saying if the ticket is not in -3 state or -2 state then show.

The other you can look at it like if state is -3 and -2 then show results then it will only show tickets that are not in either state.

Refer to (page 10 and 11)- https://www.servicenow.com/content/dam/servicenow/other-documents/training/ServiceNow-JavaScript-Pri...

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence

Thanks Mike. I learned new things.