I created state value's reason fields into incident table and whenever state value changes

ashrkml321
Giga Contributor
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   var gr= g_form.getValue('state');

    g_form.setDisplay('u_open_reason', false);
    g_form.setDisplay('u_resolve_reason', false);
    g_form.setDisplay('u_hold_reason', false);
    g_form.setDisplay('u_close_reason', false);
    g_form.setDisplay('u_new_reason', false);
    g_form.setMandatory('u_open_reason', false);
    g_form.setMandatory('u_resolve_reason', false);
    g_form.setMandatory('u_hold_reason', false);
    g_form.setMandatory('u_close_reason', false);
    g_form.setMandatory('u_new_reason', false);

    if (gr==1)
    {
        g_form.setDisplay('u_open_reason', true);
        g_form.setMandatory('u_open_reason', true);
    }
    else if (gr==2)
    {
        g_form.setDisplay('u_new_reason', true);
        g_form.setMandatory('u_new_reason', true);
    }
    else if (gr==3)
    {
        g_form.setDisplay('u_hold_reason', true);
        g_form.setMandatory('u_hold_reason', true);
    }
    else if (gr==6)
    {
        g_form.setDisplay('u_resolve_reason', true);
        g_form.setMandatory('u_resolve_reason', true);
    }
    else if (gr==7)
    {
        g_form.setDisplay('u_close_reason', true);
        g_form.setMandatory('u_close_reason', true);
    }
 
 
}
 
I created state value's reason fields into incident table and whenever state value changes respective fields should be visible and mandatory. Please help me for this...
1 ACCEPTED SOLUTION

Tarun Rekandar
Giga Guru

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:

  1. Create separate UI Policies for each state (New, Open, On Hold, Resolved, Closed).

  2. Set the condition based on the State field.(Example: state -> is not -> new).

  3. Under UI Policy Actions, configure the respective reason field:

    • Visible = false

    • Mandatory = false

  4. 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:

TarunRekandar_0-1793504463337.pngTarunRekandar_1-1793504498988.png

Example:

TarunRekandar_6-1793504694524.png

TarunRekandar_3-1793504548488.pngTarunRekandar_4-1793504577661.png

TarunRekandar_5-1793504596899.png

 

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

View solution in original post

7 REPLIES 7

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

Aditya_hublikar
Giga Sage

Hello @ashrkml321 ,

 

What's the issue are you facing in this script ?

Pallabini_S
Tera Expert

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.

function onCondition() {
if (g_form.isNewRecord()) {
g_form.setMandatory('u_new_reason', true);
g_form.setDisplay('u_new_reason', true);
g_form.setMandatory('u_open_reason', false);
g_form.setDisplay('u_open_reason', false);
g_form.setMandatory('u_resolve_reason', false);
g_form.setDisplay('u_resolve_reason', false);
g_form.setMandatory('u_hold_reason', false);
g_form.setDisplay('u_hold_reason', false);
g_form.setMandatory('u_close_reason', false);
g_form.setDisplay('u_close_reason', false);
}
}This will make sure that on New incident creation the newly created fields are not displayed.Along with it inside the on change client script on state change below code to be added.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

g_form.setMandatory('u_open_reason', false);
g_form.setDisplay('u_open_reason', false);
g_form.setMandatory('u_resolve_reason', false);
g_form.setDisplay('u_resolve_reason', false);
g_form.setMandatory('u_hold_reason', false);
g_form.setDisplay('u_hold_reason', false);
g_form.setMandatory('u_close_reason', false);
g_form.setDisplay('u_close_reason', false);
g_form.setMandatory('u_new_reason', false);
g_form.setDisplay('u_new_reason', false);

if (newValue == 1) {
g_form.setDisplay('u_new_reason', true);
g_form.setMandatory('u_new_reason', true);
} else if (newValue == 2) {
g_form.setDisplay('u_open_reason', true);
g_form.setMandatory('u_open_reason', true);
} else if (newValue == 3) {
g_form.setDisplay('u_hold_reason', true);
g_form.setMandatory('u_hold_reason', true);
} else if (newValue == 6) {
g_form.setDisplay('u_resolve_reason', true);
g_form.setMandatory('u_resolve_reason', true);
} else if (newValue == 7) {
g_form.setDisplay('u_close_reason', true);
g_form.setMandatory('u_close_reason', true);
}
}
In your code you have added :setDisplay(false) before setMandatory(false). An empty mandatory field ignores the hide. That might be the reason your code is not working for state change.