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

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

Thanks for the suggestion @Tarun Rekandar 

kmohammed
Tera Guru

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.

Thanks for the feedback. However, I have already used else if (two separate words) in my client script, not elseif. The syntax is correct.