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

When I do what you suggest in the UI Policy, the record is saved and THEN the field is set mandatory the next time the form is loaded.  At that point though, the record is already closed and no one will ever go back to look at it.

The BR suggestion got me there with a little tweak on my part using gs.addErrorMessage to draw more attention to the red color, but still does not set the field to mandatory.

I would really like to be able to get to the parent change request to get the business service from an onSubmit Client Script.  Any idea's on that?

 

Really appreciate the effort.

Conditions looks fine but Your Script Should be

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

 if(current.close_notes == ''){

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

current.setAbortAction('true');

 action.setRedirectURL(current);

}

)(current, previous);


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

Thanks for the input on this Prateek kumar.  This is mostly working, but had to change the

g_form.addInfoMessage

to

gs.addInfoMessage

Still not exactly what I was looking for...   I believe that only a Client Script "onSubmit" will work, I just never could figure out how to get the display value of the Change Record's Business Serice.  I will keep plugging away at that thought process.

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

This works perfectly!!!  Sorry for the late reply.  Been on a bit of a vacation.  Once I worked through your response and "made it my own" appropriately, it works great.

 

Thank you so much!!!!