hide a section on change request form
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
Hi @sharley ,
There are a few issues in your script:
onLoad does not use newValue; that parameter applies to onChange scripts
g_form.getValue(type) should use the field name as a string
You’re using = instead of a comparison operator
The section name must be the actual section name, not the choice value
Correct onLoad script:
function onLoad() {
var type = g_form.getValue('type'); // use the actual field name
g_form.setSectionDisplay('A', type === 'A');
}Best practice: also add an onChange Client Script on the Change Type field so the section shows/hides immediately when the value changes:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) return;
g_form.setSectionDisplay('A', newValue === 'A');
}This will ensure the section is visible only when Change Type is set to A.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
Hi @sharley ,
Your script has a few issues: onLoad doesn’t use newValue, the field name must be passed as a string, and you used = instead of a comparison.
Correct approach:
function onLoad() {
g_form.setSectionDisplay('A', g_form.getValue('type') === 'A');
}Best practice: Add an onChange Client Script on the Change Type field to toggle the section dynamically.
This will show the section only when Change Type is A.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
Hello @sharley ,
If my response helps you then mark it as helpful and accept as solution.
Regards,
Aditya,
Technical Consultant
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
You need to write the onchange and on load client scripts , aslo check if there is any Ui policy created because ui policy has more priority than client scrips:
on change cline script : this will hide the section when you field type changes
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// Replace 'type' with your actual field name if different
if (newValue === 'A') {
g_form.setSectionDisplay('your_section_name', true);
} else {
g_form.setSectionDisplay('your_section_name', false);
}
}and this in on load client script : this execute the when you open the form
function onLoad() {
var type = g_form.getValue('type');
if (type === 'A') {
g_form.setSectionDisplay('your_section_name', true);
} else {
g_form.setSectionDisplay('your_section_name', false);
}
}
i
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
you can have simply onChange which can run onLoad as well
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
var type = g_form.getValue('type');
// Replace 'type' with your actual field name if different
if (newValue === 'A') {
g_form.setSectionDisplay('sectionName', true);
} else {
g_form.setSectionDisplay('sectionName', false);
}
}
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
