Custom Approve/Reject Button on the Form

dotranmaian
Tera Contributor

I have a workflow like this

Request a inquiry form -> Waiting for the Department Manager to approve

- If approval is true -> Waiting for the General to approve

- If false -> end the flow

I have created this workflow on the Workflow Studio like this

dotranmaian_1-1744185816689.png

But the problem is, when user requested a from, the Department Manager instead of approving in My Approvals, they can directly access into the form that has been requested then approve. I have added 2 button Approve and Reject on the form but i got struggle with how to write the script for those buttons.

dotranmaian_0-1744185759546.png

 Any suggestions would be greatly appreciated.

 

 

1 REPLY 1

pratikjagtap
Giga Guru

Hi @dotranmaian ,

 

try below steps:

Table:
Set the table to the one where the inquiry form is stored (sc_req_item, u_inquiry_form, etc.)

📍 Action Type:
Form button

📍 Condition (optional, e.g., only visible to managers):

gs.hasRole('manager') || current.requested_for == gs.getUserID()

 

1. Approve Button Script (server-side):

(function executeAction(current, gForm, gUser, gSNC) {
var userId = gs.getUserID();

// Find approval record for current request and user
var approvalGR = new GlideRecord('sysapproval_approver');
approvalGR.addQuery('sysapproval', current.sys_id);
approvalGR.addQuery('approver', userId);
approvalGR.addQuery('state', 'requested');
approvalGR.query();

if (approvalGR.next()) {
approvalGR.state = 'approved';
approvalGR.comments = 'Approved via form button.';
approvalGR.update();
gs.addInfoMessage("You have approved the request.");
} else {
gs.addErrorMessage("No pending approval found for you on this request.");
}

// Refresh or redirect
action.setRedirectURL(current);
})(current, gForm, gUser, gSNC);

 

2.Reject Button Script (just change the state):

approvalGR.state = 'rejected';
approvalGR.comments = 'Rejected via form button.';

 

Optional Improvements
You could also disable/hide the buttons once approved/rejected using UI Action conditions.

Add workflow logging with workflow.info() if needed for tracking.

 

If my response helped, please hit the 👍Thumb Icon and accept the solution so that it benefits future readers.

 

Regards,
Pratik