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

Community Alums
Not applicable

Thanks so much for your reply. It does help a lot. Such a steep learning curve when trying to learn code. 

The script you gave above isn't actually checking whether the outstanding approval task is on the sysID the user is in, is it? I colleague helped out with the below code which seems to be working. I'm not moving to the UI Action script which needs to approve the task. My have to do a UI Page so it's pretty and have the user be able to enter in comments on their approval which are added. 

var gr = new GlideRecord('sysapproval_approver');
gr.addQuery('document_id', currentID);
gr.addQuery('approver', gs.getUserID());
gr.addQuery('state', 'requested');
gr.query();
if (gr.next()) {
gs.info(gr.toString());
return true; }
else {
return false;
}

Hi James,

 

Glad that it helped !!

 

The script I gave you was a kind of demo/sample code which you were supposed to modify accordingly, if you see there is no table like "approval_task" in system:)
What you are doing in above code is checking if current user is having any approval to approve on the current record where the approval task's state is Requested. 

var gr = new GlideRecord('sysapproval_approver');
gr.addQuery('document_id', currentID);
gr.addQuery('approver', gs.getUserID());
gr.addQuery('state', 'requested');
gr.query();
if (gr.next()) {
gs.info(gr.toString());
return true; }
else {
return false;
}

If this has resolved can you mark the response as correct so others will refer same in future and this will remove from unresolved thread. 🙂

Regards,

Mohammad Danish

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. 

Hi James

Thanks for sharing your solution. Nice to know to have Jelly PROs around 🙂

Have fun

Dirk

Community Alums
Not applicable
These forums are so useful I wanted to share in case it helps. I’m no expert in jelly unfortunately. Just have colleagues who helped me. Thanks for the response. All the best 👍