- 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, thanks for confirming - and fair point. The code as pasted in this post renders as "elseif" (the forum strips the spacing when code is pasted without the code-sample formatting), so I read it exactly as it appears here. If the script in your instance already uses "else if", syntax is not the blocker, so please disregard that part.
With the syntax correct, the next most likely reason this onChange script appears to do nothing on Incident is the field it is attached to. On the Incident form, the State control the user actually changes is incident_state, not state. The two fields are kept in sync, but an onChange client script whose Field is set to "state" does not fire when a user picks a new value in that form control, because "state" itself is not the field being edited on the form. Change the client script's Field to incident_state and test again - and inside the script, compare newValue directly (it is the value just selected) rather than g_form.getValue('state').
Two smaller things to verify while you are there:
1. The 1/2 mapping looks swapped against your field names: state 1 is New, so it should show u_new_reason; state 2 is In Progress, which matches u_open_reason. As posted, state 1 shows u_open_reason and state 2 shows u_new_reason - if you tested with New while expecting the new-reason field, that alone would look like a failure.
2. If it still does not trigger after switching the field, temporarily put g_form.addInfoMessage(newValue); at the top of the script. If the message never appears, the script is not running at all (check it is Active, on the incident table, and applies to the view you are testing in). If the message appears but the fields do not change, the five u_ fields are not in the form view being tested - setDisplay cannot show a field that is not on the form.
Regards,
Karimuddin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Seems like you have to create an UI Policy with out any condition,onLoad checked.Check run script and inside execute if true, the below code may get added.