- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2023 07:12 PM - edited 11-13-2023 07:13 PM
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!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2023 07:46 PM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2023 08:04 PM
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 👍✅
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2023 08:19 PM
Ankur,
You were probably right first, but AnveshKumar explained it in a way that I understood my mistake. Your response was still helpful, sir.
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2023 09:05 PM
Glad to help.
I just tried to explain the difference and use as per your requirement
As per new community feature you can mark multiple responses as correct.
If my response helped please mark it correct as well so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2023 08:01 PM
Hi @PLakin1
Didn't worked in the sense, is it not working in it's entirety or just the some parts. The behaviour I expect from the second script which didn't worked for you is:
- It will make the variables editable if the Catalog Item is the one specified and state is either 1 or 2.
- If the catalog Item is not the one specified, irrespective of state the variables become read only.
- if the catalog item is the one specified but state is neither 1 nor 2 , it will not make any difference, it will be just the default behaviour of the form (this the difference between the first and second script).
Where as with the first script, the behaviour is:
- Make variables editable if the catalog item is the one specified and state is either 1 or 2.
- Make the variables read only if the catalog item is not the one specified or the state is not one of 1 and 2.
To make the second script behave like the first one do the following modifications.
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 👍✅
Anvesh