Approval button for individual outstanding tasks

Community Alums
Not applicable

Hi All

I have a requirement to add an Approval button to a form. The button should only be visible if the user has an outstanding approval task on that form. 

I'm fairly new to scripting. I've done UI Action approval buttons before, but they were done with a condition to say when to show it, and it would move the whole ticket along. In this scenario i just want the approval button to approve the approval task of the user who clicks approves. 

I'm kinda of lost. Do i need a business rule, a script include?? 

any suggestions would be greatly appreciated. 

1 ACCEPTED SOLUTION

Community Alums
Not applicable

So I ended up doing a UI Page for a pop up to capture comments on the approval or reject buttons. 

The UI Action script simply calls the UI Page which has the meat of the script in it. 

UI Action:

function onClickApprove(){
	var dialog = new GlideDialogWindow("approvalComments");
	dialog.setTitle('Please enter comments to approve this procurement');
	dialog.setSize(450,550); //Set the dialog size
	dialog.removeCloseDecoration(); //remove the close cross from the dialogwindow
	dialog.render();
}		

The UI Page script:

function updateApproval(action) {
	var textArea = gel('approval_comments'); //Pass the comment from the user
	textArea = textArea.value.trim();
	var sys_id = g_form.getUniqueValue(); // Get the sys_id of the procurement
	var approvaRec = new GlideRecord('sysapproval_approver'); //Approval table
	approvaRec.addQuery('document_id', sys_id);
	approvaRec.addQuery('approver', g_user.userID); // Get the current user ID
	approvaRec.addQuery('state', 'requested');
	approvaRec.query();
	if (approvaRec.next()) {
		approvaRec.state = 'approved'; //
		approvaRec.comments = textArea; // Get the value from the gel object and trim.
		approvaRec.update();
		if ((g_form.getValue('** FIELD NAME OF STAGE WE WANTED TO CHECK **') == '** THE VALUE OF THE FIELD ** ' ) || (g_form.getValue('**ANOTHER FIELD NAME**') == '**VALUE OF FIELD**')) {
			g_form.setValue('u_neg_directive', textArea); 
		g_form.save();  }
		
				gsftSubmit(null, g_form.getFormElement(), "sysverb_update_and_stay"); //Save and Stay
		}
		
	}


(function() {
	$("approval_comments").focus();
})();

The UI Page HTML looks like this: 

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
	<g:dialog_notes_ok_cancel
		dialog_id="change_confirm_cancel"
		textarea_id="approval_comments"
		textarea_label="${gs.getMessage('')}"
		textarea_label_title="${gs.getMessage('Approval comments are required')}"
		textarea_name="approval_comments"
		textarea_onkeyup="enableButton()"
		textarea_style="height:auto; width: auto;"
		textarea_title="${gs.getMessage('Enter your comments here')}"
		ok=""
		ok_action="updateApproval"
		ok_id="change_confirm_ok_btn"
		ok_title="${gs.getMessage('')}"
		ok_type="button"
		ok_style_class="btn btn-primary disabled"
	/>
</j:jelly>

 

Hope this helps someone. 

View solution in original post

10 REPLIES 10

DirkRedeker
Mega Sage

Hi

I recommend to ceate a UI Action with a Script as condition. In the Script, GlideRecord to the "sysapprovel_approver" table to find, if there are any records in "sysapproval_approver" to point to your current record (the reference the current table each).

If there are still any open Approvals for the current record, show the button - otherwise hide it.

Let me know if that answers your question and mark my answer as correct/helpful

BR

Dirk

Mohammad Danis1
Giga Guru

Hi James,

The UI action is only going to display if the Condition field evaluates to true. The evaluation is done on the server side.

Please try below code :

Create a script include called "hasApprovalTask" with a simple function that detects if current user has approval Task or not.

function hasApprovalTask() {
loggUser = gs.getUserID();
gs.addInfoMessage('Logged In User :'+loggUser);
var gp = new GlideRecord('approval_task');
gp.addQuery('approval',loggUser);
gp.query();
if(gp.next())
{
return true;
}
return false;
},      

 

Then your condition statement just calls this function, Warning: This is untested code 🙂

 

Condition: new scriptincludename().hasApprovalTask()

 

http://wiki.servicenow.com/index.php?title=UI_Actions

 

Let me know, if that answers your question and mark my answer as correct / helpful

Regards,

Mohammad Danish

Please find the updated Code Above 🙂

Hey James,

Is this answered?

If Yes, can you please mark response as correct so others will refer same in future and this will remove from unresolved thread.

Regards,

Mohammad Danish