how to create Approve and Reject buttons in custom table

Amol Pawar
Tera Guru

Hi everyone,

 

I want to create two buttons on the form of a custom table named 'Approve' and 'Reject'. I've created UI actions for that but need help with the script. 

If anyone has anything to refer to, please share it with me. And once the approver approves or rejects, buttons should disappear from the form.

I've created a flow for approvals which requires two approvers to approve the request. When the first approver approves or rejects the request the buttons should disappear for him but be visible to another approver.

 

Thanks in advance,

Amol

1 REPLY 1

Weird
Mega Sage

How have you done it?
Have you created the approvals in your flow so that they exist in the sysapproval_approver table?
If so, your buttons could check in their condition if the currently logged in user has an approval record for the current record in requested state. If they do then the button is shown.
Approval button should change the state of the approval to approved and rejection to rejected.
The button would automatically disappear from them after pressing it as the state is changed.

If you create the second approval after the first is approved then the second approver will see their approval buttons only after the first one has approved.

Basically your condition will call a script include which returns true or false. True to show and false to hide.
For example you could create a script include called "ApprovalUtil" and a method "approvalOpen".
UI action condition: new ApprovalUtil().approvalOpen(current.sys_id);

Then your script includes ApprovalUtil  method approvalOpen is called to check whether to show the UI action

approvalOpen: function(sys_id){
var approvals = new GlideRecord('sysapproval_approver');
approvals.addQuery("sysapproval", sys_id);
approvals.addQuery("state", "requested");
approvals.addQuery("approver", gs.getUserID());
approvals.query();
if(approvals.next()){
return true;
}
return false;
}


Then your UI action just has to do the same query and depending on which button it is on it'll update the approval state:

var approvals = new GlideRecord('sysapproval_approver');
approvals.addQuery("sysapproval", sys_id);
approvals.addQuery("state", "requested");
approvals.addQuery("approver", gs.getUserID());
approvals.query();
if(approvals.next()){
approvals.state = "approved";
approvals.update();
}


For rejection the state should be "rejected".


Note that I just wrote this without referencing any values, so you need to make sure all the field names and choice values are correct before implementing this.