|
| 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); |
0 commit comments