Trying to make some mandatory fields not mandatory when Cancel button is clicked on Change Form

prudhvig
Tera Expert

Hi,

As of now, the change request in my instance could be canceled in two ways :

1.) Moving the State to Canceled

2.) Cancel Button on the form (not the OOB cancel change. I have created my own according to my requirement).

I want 5 of the fields not to be mandatory on the form while clicking the cancel button or moving state to Canceled.

Those fields are shown in the snapshot below :

Screenshot (46)_LI.jpg

I have edited my UI Policy so that these fields would be mandatory in all states except Canceled. This does not show those fields as mandatory when the state moves to Canceled.

But, when I open a new change form (not filling any mandatory fields) and click the cancel button, I am being asked to fill in the ones (the five fields shown above) which I thought should not be mandatory while canceling.

Screenshot (47)_LI.jpg

Do I need to add extra code to the UI Action so that when cancel button is clicked, it would ask to fill other mandatory fields but not these five?

In that case, my UI Action code reads the following :

function cancelChange(){

  if(!g_form.isMandatory('work_notes')){

  g_form.setMandatory('work_notes',true);

  g_form.addErrorMessage('Work Notes is Mandatory while canceling change');

  return "";

  }

  gsftSubmit(null, g_form.getFormElement(), 'AST:AST:CHG-CancelButton');

}

if (typeof window == 'undefined'){

  current.state = 4;

  action.setRedirectURL(current);

  current.update();

}

Please guide me in solving this. Thanks in advance.

1 ACCEPTED SOLUTION

marcguy
ServiceNow Employee
ServiceNow Employee

can you add this line to move state to cancelled immediately in the client, then the UI policy will not enforce the mandatory fields....



function cancelChange(){


g_form.setValue('state',4);


  if(!g_form.isMandatory('work_notes')){


  g_form.setMandatory('work_notes',true);


  g_form.addErrorMessage('Work Notes is Mandatory while canceling change');


  return "";


  }



  gsftSubmit(null, g_form.getFormElement(), 'AST:AST:CHG-CancelButton');


}




if (typeof window == 'undefined'){


  current.state = 4;


  action.setRedirectURL(current);


  current.update();


}


View solution in original post

4 REPLIES 4

marcguy
ServiceNow Employee
ServiceNow Employee

in the client part can you make state 'cancelled' there, then the ui policy condition won't fire



g_form.setValue('state','cancelled');


Thanks for the reply mguy, but I did not understand what you were trying to say. Could you please put it more detailed as I am very new to Servicenow


marcguy
ServiceNow Employee
ServiceNow Employee

can you add this line to move state to cancelled immediately in the client, then the UI policy will not enforce the mandatory fields....



function cancelChange(){


g_form.setValue('state',4);


  if(!g_form.isMandatory('work_notes')){


  g_form.setMandatory('work_notes',true);


  g_form.addErrorMessage('Work Notes is Mandatory while canceling change');


  return "";


  }



  gsftSubmit(null, g_form.getFormElement(), 'AST:AST:CHG-CancelButton');


}




if (typeof window == 'undefined'){


  current.state = 4;


  action.setRedirectURL(current);


  current.update();


}


Thank you very much mguy. It worked.