OnSubmit Client Script - Force Mandatory

jstocton
Kilo Expert

I have the following use case:  

On the change_task form, the close_notes will be mandatory OnSubmit -- IF -- the business_service field on the parent change_request STARTSWITH SAP(space).

I was going to do this as a UI Policy, but there is no way to make the field mandatory on change or on submit.  I am really perplexed and have not seen any good examples of how to get to the parent change record and validate the contents of a field.  I know this is simple but just can't seem to make it work.

1 ACCEPTED SOLUTION

So in that case you can do something like this:

1. Create a Display business rule and capture the value of business service in g_scratchpad variable. Your BR would like this.

Display BR

Script:

(function executeRule(current, previous /*null when async*/) {

var ciName = current.parent.cmdb_ci.getDisplayValue(); //change cmb_ci to your Business service Name
g_scratchpad.ciName = ciName;

})(current, previous);

 

2. Use the scratchpad value in your OnSubmit Client Script:

function onSubmit() {
alert(g_scratchpad.ciName);
var str = g_scratchpad.ciName;
if(str.substring(0,4) == 'bond'){ //change bond to 'SAP '
alert('Cannot happen');
g_form.setMandatory('description',true); // replace description with your required field value
return false;
}
}


Please mark my response as correct and helpful if it helped solved your question.
-Thanks

View solution in original post

10 REPLIES 10

Prateek kumar
Mega Sage

Why not use a Before BR on change_task table?


Please mark my response as correct and helpful if it helped solved your question.
-Thanks

The SN Nerd
Giga Sage
Giga Sage

One quick way would be to expose the Parent.Business Service field on the Change Task.

Then, you could use UI Policy (You can only dot walk on UI Policy if that field is on the form).


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

jstocton
Kilo Expert

The close notes should only be required on close when the change_request business service starts with SAP.  A UI Policy will set it regardless of when.  Meaning when the form loads and the business service starts with SAP...even if I'm not ready to close the task I would be required to fill in close notes.  That won't work.

The BERORE (insert/update) Business Rule seems a bit closer but can't get the field to mandatory.

Conditions are:

Change request.Business service  |  starts with  |  SAP  (AND)

State  |  changes to  |  Closed Complete  (or) Closed Incomplete (or) Closed Skipped

 

Nothing on the Actions Tab

Advanced Tab (no condition)

Script as follows:

(function executeRule(current, previous /*null when async*/) {

  // Add your code here

  g_form.setMandatory(close_notes,true);

  current.update();

  g_form.addInfoMessage('This will be the very informative message');

   action.setRedirectURL(current);

}

)(current, previous);

Could you not just add 'Status' IS ONE OF 'Closed Complete, Closed Incomplete, Closed Skipped' to the UI Policy?

In regards to your business rule, you are mixing Client Side API with Server Side API.
Business rules do not use g_form or action, and cannot be used to make fields explicitly mandatory.

(function executeRule(current, previous /*null when async*/) {

  // Add your code here
  current.setAbortAction('true');
  gs.addInfoMessage('This will be the very informative message');
   gs.setRedirectURL(current);

}

)(current, previous);

 


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022