- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2020 10:28 AM
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.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2020 10:43 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2020 10:43 AM
Hi,
The line g_form.getValue('state') != '-3' || g_form.getValue('state') != '-2' will check for state should not be -3 or state should not be -2.
The line g_form.getValue('state') != '-3' && g_form.getValue('state') != '-2' will check for state should not be -3 and -2 as well.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-02-2020 02:15 AM
Thanks for reply Onkar.