UI action button should be visible only if no approver has approved.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-31-2016 08:03 AM
Hi all,
I have a situation where UI button should not be visible on the form if the approvers related list contains any record as 'approved'. i have written the below script but how do i check it with related lists?
Ui action script is below. what else should i add in the script to check all records in the related list?
Script:
function rollback_change(){
g_form.setValue('u_rollback_from_review_approval', true);
g_form.setValue('u_change_phase', 0);
g_form.setValue('approval', 'not requested');
g_form.save();
}
Condition:
current.u_change_phase == "1" && current.u_reopened==false && current.approval=='requested' && (gs.getUserID()==current.requested_by || gs.getUserID()==current.u_change_owner || gs.getUser().hasRole('itil'))

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-31-2016 11:10 AM
The UI action is only going to display if the Condition field evaluates to true. The evaluation is done on the server side. Here's how I recommend doing this:
Create a script include called "hasNoApproved" with a simple function (no class/prototype stuff) that detects if the current record has any related approvals set to 'approved'. Warning: This is untested code>
function hasNoApproved(gr) {
var app = new GlideAggregate('sysapproval_approver');
app.addAggregate('COUNT');
app.addQuery('sysapproval', gr.getValue('sys_id'));
app.addQuery('state', 'approved');
app.query();
var count = -1;
if (app.next()) {
count = app.getAggregate('COUNT');
}
return count == 0;
}
Then your condition statement just calls this function
Condition: hasNoApproved(current)
See episode 6 for an example of this: TechNow Episode List
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-31-2016 12:23 PM
Thanks Chuck I'll try that.
Also, i've tried the below Ui action to restart workflow on change request table. It is not client side script and i've unchecked 'client'. It does restart the workflow but on click it redirects me to list of change records instead of going to 'change phase' = initial. Can you suggest where i'm going wrong here please?
var gr = new GlideRecord('sysapproval_approver');
gr.addQuery('sysapproval', current.sys_id);
gr.query();
while (gr.next())
{
gr.deleteRecord();
}
new Workflow().restartWorkflow(current, true);
current.u_change_phase = "0"; //should take me to initial phase on saving the form.
current.update();

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-31-2016 01:21 PM
By default, a UI action is going to take you from the record to the list of records (or indicated previous screen on the sysparm stack.) If you want to stay on the same record, use this right after your current.update();
action.setRedirectURL(current);
This is explained in the UI action page.
http://wiki.servicenow.com/index.php?title=UI_Actions

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-31-2016 03:00 PM
If I have answered your question, please mark my response as correct so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list.
If you are viewing this from the community inbox you will not see the correct answer button. If so, please review How to Mark Answers Correct From Inbox View.
Thank you