Onload Client Script to make Variable mandatory is getting clear on load of form
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2024 10:36 AM - edited 09-29-2024 10:39 AM
Hi All,
I have written Onload Client Script to make variable "is_this_it_sponsored" within the variable editor mandatory based on catalog task short description. When I enter the value and close the task. Then after the form reloads the value is getting clear and again showing mandatory .Can you all please help me with this.
function onLoad() {
if (g_form.getValue('short_description') == 'Review request details') {
g_form.setMandatory('variables.is_this_it_sponsored', true);
} else if (g_form.getValue('short_description') == 'Conduct Technical evaluation' ||
g_form.getValue('short_description') == 'Review integration request & schedule discovery meeting') {
g_form.setMandatory('variables.operational_impact', true);
g_form.setMandatory('variables.is_this_it_sponsored', false);
g_form.setDisplay('variables.is_this_it_sponsored', false);
} else if (g_form.getValue('short_description') == 'Conduct AI review' || g_form.getValue('short_description') == 'Conduct Cloud review' ||
g_form.getValue('short_description') == 'Conduct Infosec review') {
g_form.setDisplay('variables.operational_impact', true);
g_form.setReadOnly('variables.operational_impact', true);
} else {
g_form.setMandatory('variables.operational_impact', false);
g_form.setDisplay('variables.operational_impact', false);
g_form.setMandatory('variables.is_this_it_sponsored', false);
g_form.setDisplay('variables.is_this_it_sponsored', false);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2024 10:05 AM
Thanks for confirming that. So only with this script activated, which is only controlling the mandatory and visibility of two variables, when you select a choice on one of the variables and save/submit, the value is missing when the form reloads?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2024 11:38 PM
Two options and both should work:
1. Write 4 catalog UI policies for your 4 if/else and just check the field "Applies on Catalog Task" only. (Rest unchecked)
2. Write one catalog client script same like above, but dont use variables.<name> simple use <name> only. and check the field "Applies on Catalog Task"only. (Rest unchecked)
Post this, open script tracer, start it and perform the form saving again and observe what all happens in the tracer logs and see which other configuration is impacting this and clearing the value.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2024 10:14 AM - edited 09-30-2024 11:00 AM
@Brad Bowman Yes Correct
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2024 12:10 PM
It sounds like you are testing on a Catalog Task form. Do you have the box checked to apply only on Catalog Tasks, or also Request Items? When you are conducting your test are you setting the value on a Catalog Task, then saving it and the same Catalog Task form is reloaded, or is it the RITM record that appears with the variable empty and mandatory?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2024 11:23 PM
Hi @ukti ,
Solutions to Prevent Clearing the Variable Value:
1. Make the Field Visible When Mandatory:
If a field is marked mandatory, it should be visible to avoid ServiceNow clearing its value. You can modify your script to ensure that mandatory fields are displayed during form reloads.
2. Retain the Field Value Using GlideRecord:
Ensure the field value is retained in the system when the task is closed or reloaded. You may want to consider a GlideRecord update on the variable's value to save it before the field is hidden again.
Updated OnLoad Client Script:
Here’s an updated version of your script that ensures the field value isn’t cleared during form reloads by keeping the field visible when it’s mandatory:
function onLoad() {
var shortDesc = g_form.getValue('short_description');
if (shortDesc == 'Review request details') {
// Make the variable mandatory and visible
g_form.setMandatory('variables.is_this_it_sponsored', true);
g_form.setDisplay('variables.is_this_it_sponsored', true); // it is visible when mandatory
} else if (shortDesc == 'Conduct Technical evaluation' ||
shortDesc == 'Review integration request & schedule discovery meeting') {
g_form.setMandatory('variables.operational_impact', true);
g_form.setMandatory('variables.is_this_it_sponsored', false);
// Show impact but hide is_this_it_sponsored
g_form.setDisplay('variables.operational_impact', true);
g_form.setDisplay('variables.is_this_it_sponsored', false);
} else if (shortDesc == 'Conduct AI review' || shortDesc == 'Conduct Cloud review' ||
shortDesc == 'Conduct Infosec review') {
// Set to readonly and visible
g_form.setDisplay('variables.operational_impact', true);
g_form.setReadOnly('variables.operational_impact', true);
} else {
// Make sure both variables are not mandatory or visible if none of the conditions are met
g_form.setMandatory('variables.operational_impact', false);
g_form.setDisplay('variables.operational_impact', false);
g_form.setMandatory('variables.is_this_it_sponsored', false);
g_form.setDisplay('variables.is_this_it_sponsored', false);
}
}
Key Changes:
- Always Display Mandatory Fields: When the `is_this_it_sponsored` variable is set to mandatory, it is now always displayed to prevent ServiceNow from clearing its value during form reload. Hidden mandatory fields can cause issues.
- Hide the Field Only When Not Mandatory: The field is hidden only when it is not mandatory, ensuring that the value remains intact.
Thanks and regards,
Siddhesh Jadhav
I hope this helps! If this resolves your issue, kindly mark my answer as helpful and accepted.