- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-27-2017 06:59 AM
I've read many posts about this and followed SN Guru posting about this as well. I've tried several versions of the script below and it still won't work:
This is an onChange client script - supposed to hide a section unless Category = XVALUE. Here's my script (info messages are just so I can see what the script is doing). What have I done wrong? The section will not be hidden - shows no matter what. I've also tried as onLoad script - still no dice. Any help greatly appreciated! The info messages, btw, behave as expected. The section will not hide though.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === 'XVALUE') {
return;
}
//Type appropriate comment here, and begin script below
var sections = g_form.getSections();
var val = g_form.getValue('category');
g_form.addInfoMessage(val);
if (val == 'Firewall'){
g_form.addInfoMessage('Show ' + val);
g_form.setSectionDisplay('firewall_info', true);
}
else{
g_form.addInfoMessage('Do not show ' + val);
g_form.setSectionDisplay('firewall_info', false);
}
}
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-27-2017 09:40 AM
Hi Dale,
Simple suggestion is to create a UI policy and add the condition if the change type is so and so and in the UI policy add the variables and make it mandatory true and check reverse if false is checked. So obviously these fields become mandatory false and this client script will also execute.
Or in the UI policy script itself you can write a script execute if true to make the fields mandatory and section visible and in the execute if false, make them mandatory false and make the section visibility as false

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-27-2017 09:40 AM
Hi Dale,
Simple suggestion is to create a UI policy and add the condition if the change type is so and so and in the UI policy add the variables and make it mandatory true and check reverse if false is checked. So obviously these fields become mandatory false and this client script will also execute.
Or in the UI policy script itself you can write a script execute if true to make the fields mandatory and section visible and in the execute if false, make them mandatory false and make the section visibility as false
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-27-2017 10:20 AM
This would also be my approach.
Your problem isn't the script, your problem is wanting to hide something on a form - UI Policies are a preferred solution over scripting.
(by the way, what's the purpose of your sections variable?)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-27-2017 11:26 AM
Ha! I'm very new to scripting - and learning along the way along with ServiceNow. The sections variable is probably a vestige of one of the several attempts and approaches to make the script work - didn't get deleted or commented out and it didn't cause a problem, so it stayed. It's a huge step up the slippery learning curve that I actually see what you mean
Also, I started out with a UI policy and didn't see a way to apply it to a Section, so went straight to client script after some searching and reading SN Guru's post. I agree, UI Policy sounds like the way to go, though.
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-27-2017 11:45 AM
Heh - I guessed you may be new to it. Are you new to programming, or just new to scripting in the platform?
Couple of platform tips:
- try server-side methods over client-side, e.g.: business rules, data policies, ACLs first
- try client-side non-scripted before scripted, i.e.: make client-side scripting your last resort, rather than first port of call.
(these are tips I discuss when I deliver Scripting in Service Now courses)
Couple of programming tips: if you're going to add debug lines in that quickly show the value of something, e.g.:
g_form.addInfoMessage(val);
consider using alert() instead:
alert(val);
- one advantage is that processing stops until you click "okay", so you can use this call as a breakpoint.
Also, precede the variable with some text, so that if the variable is blank/empty, you at least see the text being written out, e.g.:
alert("val is currently: " + val);
For client-side scripting, I normally have two separate browsers open: I code in one, then refresh in the other to see the affect. It helps watching the actions and matching them against code lines.
Hope that helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-27-2017 12:17 PM
Wow - thanks for the tips! Very helpful. I'm new to both. Feeling overwhelmed, but also excited. I've wanted to learn this stuff for a while and found it hard while on the job. Since switching to ServiceNow it's part of my job.