- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-21-2016 07:39 AM
I have two fields that need to be required in order to "Close Complete" a task. I can do that with a UI Policy for the Close Complete "State" value, however the "Complete Task" button will defeat the UI Policy and simply set the "State" to "Close Complete" without enforcing the requirement of the two fields.
To complicate matters, I only need the fields to be required for about half (20) of our Assignment Groups. So, I have one UI Policy that hides the two fields for the other 20 groups and a second one that displays and requires the two fields in order for "Closed Complete" to be the "State."
What are your thoughts on what I should do about the "Complete Task" button? I poked around the community a bit and haven't really found something like this.
I was thinking maybe modifying the UI Action with a Client Side check and then a Server Side action to Close Complete if the check shows True for the Assignment Group…whether the Assignment Group is one of the required ones or not. I just have no idea how to start something like that. Plus would that involve coding in every sys_id for each Assignment Group?
Is there a way to force the UI Action to enforce the UI Policies before it runs?
thanks,
Richelle
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-21-2016 07:48 AM
Hi Richelle,
The only way to make the ui action respect your ui script is to run it as a client/server action like you suspected. This is a good article to start.
Client & Server Code in One UI Action - ServiceNow Guru
You could also use a data policy which would be enforced on the server, but I don't think it's as good a user experience. You really want to give the user feedback before the form is submitted.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-21-2016 07:48 AM
Hi Richelle,
The only way to make the ui action respect your ui script is to run it as a client/server action like you suspected. This is a good article to start.
Client & Server Code in One UI Action - ServiceNow Guru
You could also use a data policy which would be enforced on the server, but I don't think it's as good a user experience. You really want to give the user feedback before the form is submitted.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-21-2016 10:39 AM
Thanks Brad,
That article was very helpful.
Here's what I have now...alas, it does not work. I've tried a few different tweaks to the code, but I'm missing something obvious (I'm sure). I have a field on the Assignment Group form called "u_require_sp_sa" and I am referencing it on the Task form. I thought I would use that as one of the criteria for making the "u_checklist" field required and a pop-up message to appear.
The u_checklist field isn't actually empty...the value "-- None --" is in the field. (I tried the code with that instead of '' as well, but no go.
Anything look obvious to you? When I click on the "Complete Task" button now, nothing happens...
thanks, Richelle
Name: Complete Task
Action Name: SPSA
Onclick: checkSPSA
Condition: (current.state < 3 && current.approval != 'requested' && gs.hasRole('itil'))
Script:
//Check Required SPSA Fields
function checkSPSA(){
if (g_form.getValue('current.sc_task.assignment_group.u_require_sp_sa') == 'true' && g_form.getValue('u_checklist') == '') {
g_form.hideFieldMsg('u_checklist');
g_form.setMandatory('u_checklist', true);
g_form.showFieldMsg('u_checklist', 'Service Provided and Service Activity values are required.', 'error');
return false; //Abort Submission
}
//Call the UI Action and skip the 'onclick' function
gsftSubmit(null, g_form.getFormElement(), 'SPSA'); //MUST call the 'Action name' set in this UI Action
}
//Code that runs without 'onclick'
//Ensure call to server-side function with no browser errors
if(typeof window == 'undefined'){
SPSA();
}
//Server-side function
function SPSA(){
- current.state = 3;
- current.closed_at = gs.nowDateTime();
- current.assigned_to = gs.getUserID();
- current.update();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-21-2016 10:52 AM
I found it. And it was obvious. And easy to miss.
Up in the On Click box I needed to have...
Onclick: checkSPSA();
Once I did that the code worked like a charm.
Thanks Brad, I'll mark your answer as correct for the great article lead...
Richelle