Skip to content

Commit 8d14e10

Browse files
authored
Approve on behalf Scripted Rest API (#1489)
* Create approval_on_behalf.js Added code for Scripted Rest API * Create readme.md Added Readme.md file
1 parent ce420ac commit 8d14e10

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* Approve On Behalf Scripted REST API
3+
* This API allows authorized users to approve or reject tasks on behalf of another user.
4+
* It handles impersonation, performs actions on approval records, and returns appropriate responses.
5+
*
6+
* @param {RESTAPIRequest} request - The request object containing data from the client
7+
* @param {RESTAPIResponse} response - The response object to send data back to the client
8+
*/
9+
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
10+
try {
11+
// Parse request data
12+
var reqData = JSON.parse(request.body.dataString);
13+
var reqId = reqData.approvalRecId;
14+
var user = reqData.userId;
15+
var action = reqData.action;
16+
var comments = reqData.comments;
17+
18+
// Validate input
19+
if (!reqId || !user || !action) {
20+
return respondWithError(response, "Missing required fields: approvalRecId, userId, or action.");
21+
}
22+
23+
// Check impersonation rights
24+
var canImpers = new GlideImpersonate().canImpersonate(user);
25+
if (!canImpers) {
26+
return respondWithError(response, "Cannot impersonate user " + user);
27+
}
28+
29+
// Impersonate the user
30+
var impUser = new GlideImpersonate();
31+
impUser.impersonate(user);
32+
33+
// Fetch the approval record
34+
var approvalGR = new GlideRecord('sysapproval_approver');
35+
if (!approvalGR.get(reqId)) {
36+
return respondWithError(response, "Invalid approval record ID: " + reqId);
37+
}
38+
39+
// Perform action based on the request (approve/reject)
40+
if (action.toLowerCase() === 'approve') {
41+
approvalGR.state = 'approved';
42+
} else if (action.toLowerCase() === 'reject') {
43+
approvalGR.state = 'rejected';
44+
} else {
45+
return respondWithError(response, "Invalid action specified. Valid actions are 'approve' or 'reject'.");
46+
}
47+
48+
// Add comments if provided
49+
if (comments) {
50+
approvalGR.comments = comments;
51+
}
52+
53+
// Update the record
54+
approvalGR.update();
55+
56+
// Response success
57+
response.setStatus(200);
58+
response.setHeader('Content-Type', 'application/json');
59+
response.setBody({ "success": true, "message": "Action '" + action + "' performed successfully on approval record." });
60+
61+
} catch (e) {
62+
// Handle errors and respond
63+
respondWithError(response, "An error occurred: " + e.message);
64+
}
65+
66+
/**
67+
* Helper function to respond with error
68+
* Sends a consistent error response to the client with a status of 400.
69+
*
70+
* @param {RESTAPIResponse} response - The response object to send data back to the client
71+
* @param {string} message - The error message to respond with
72+
*/
73+
function respondWithError(response, message) {
74+
response.setStatus(400);
75+
response.setHeader('Content-Type', 'application/json');
76+
response.setBody({ "success": false, "message": message });
77+
}
78+
})(request, response);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Approve On Behalf - Scripted REST API
2+
3+
## Overview
4+
This REST API allows authorized users to approve or reject tasks on behalf of another user. The script handles impersonation, performs action on approval records, and returns appropriate responses based on the success or failure of the request.
5+
6+
### API Definition
7+
- **Name**: Approve On Behalf
8+
- **Application**: Global
9+
- **Active**: Yes
10+
- **HTTP Method**: POST
11+
- **Relative Path**: /
12+
- **Resource Path**: /api/aueis/approve_on_behalf
13+
14+
## Request Format
15+
The API accepts `application/json` as the input format.
16+
17+
### Sample Request
18+
```json
19+
{
20+
"approvalRecId": "1234567890abcdef",
21+
"userId": "user.name",
22+
"action": "approve",
23+
"comments": "Approving on behalf of the user"
24+
}
25+
26+
27+
### Sample Success Response
28+
json
29+
Copy code
30+
{
31+
"success": true,
32+
"message": "Action 'approve' performed successfully on approval record."
33+
}
34+
### Sample Error Response
35+
json
36+
Copy code
37+
{
38+
"success": false,
39+
"message": "Invalid approval record ID: 1234567890abcdef"
40+
}

0 commit comments

Comments
 (0)