- 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 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 07:58 PM - edited 11-13-2023 07:59 PM
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.
- 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:18 PM
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!