- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @ashrkml321,
The best approach for this requirement would be to use UI Policies instead of writing a Client Script.
Since your requirement is to display the respective reason fields and make them mandatory based on the Incident State value, you can achieve this using UI Policies without writing any JavaScript.
ServiceNow is a low-code/no-code platform, so whenever a requirement can be handled using out-of-the-box configurations, it is better to prefer them over scripting.
Suggested approach:
Create separate UI Policies for each state (New, Open, On Hold, Resolved, Closed).
Set the condition based on the State field.(Example: state -> is not -> new).
Under UI Policy Actions, configure the respective reason field:
Visible = false
Mandatory = false
Use the same condition and approch for all the state.
How it works: when the state is not new the field will be hidden, only the field appear to that state new.
condition:
Example:
This approach makes the configuration easier to maintain, reduces unnecessary scripting, and follows ServiceNow best practices.
Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards,
Tarun
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @ashrkml321,
The best approach for this requirement would be to use UI Policies instead of writing a Client Script.
Since your requirement is to display the respective reason fields and make them mandatory based on the Incident State value, you can achieve this using UI Policies without writing any JavaScript.
ServiceNow is a low-code/no-code platform, so whenever a requirement can be handled using out-of-the-box configurations, it is better to prefer them over scripting.
Suggested approach:
Create separate UI Policies for each state (New, Open, On Hold, Resolved, Closed).
Set the condition based on the State field.(Example: state -> is not -> new).
Under UI Policy Actions, configure the respective reason field:
Visible = false
Mandatory = false
Use the same condition and approch for all the state.
How it works: when the state is not new the field will be hidden, only the field appear to that state new.
condition:
Example:
This approach makes the configuration easier to maintain, reduces unnecessary scripting, and follows ServiceNow best practices.
Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards,
Tarun
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Thanks for the suggestion @Tarun Rekandar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi, the script will not run at all as written, and that is the main problem. JavaScript does not have an "elseif" keyword, so every "elseif" in your script has to be "else if" (two words). With "elseif", the whole client script fails with a syntax error and none of the setDisplay or setMandatory calls execute.
Change each block like this:
else if (gr == 2) {
g_form.setDisplay('u_new_reason', true);
g_form.setMandatory('u_new_reason', true);
}
Do the same for states 3, 6, and 7. Your first "if (gr == 1)" block is fine, and the rest of the logic, hiding all five reason fields first and then showing only the one for the current state, is the right pattern.
Two more things to check after fixing that:
1. Make sure this client script is on the Incident table, with Field name set to state, Type set to onChange, and Active checked. An onChange script only runs when the state field itself changes, so it will not apply the rule to a record that is simply opened in that state.
2. Make sure all five reason fields are actually on the Incident form layout. setDisplay can only show a field that is on the form; a field that exists only on the table will stay hidden even when the script runs.
Fix the "elseif" lines first, then change the state on an incident and the matching reason field should appear and become mandatory.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Thanks for the feedback. However, I have already used else if (two separate words) in my client script, not elseif. The syntax is correct.