Calling a REST API flow action into the server side/client side script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2024 08:12 AM
Need to create a new Flow Action to invoke the REST API and call it into the widget.
The flow action should call the REST API (API is used to download the document from DMS.)and return the sys_id of the newly created attachment as an output.
Not able to achieve it. Please find the details which I tried below and help me on this
(task id is object id and user id is login name of the user from sys user)
Flow action script:
Widget server script:
if (input.action == "downloadLink") {
var taskNumber;
var approval_gr = new GlideRecord("sysapproval_approver");
approval_gr.addQuery("sys_id", input.approval_sys);
approval_gr.orderByDesc("sys_created_on");
approval_gr.query();
if (approval_gr.next()) {
var taskId = approval_gr.document_id;
taskNumber = approval_gr.document_id.number;
}
try {
// Step 1: Define the inputs to pass to the Flow
var inputs = {
'approval_sys': input.approval_sys,
'taskId': taskId
};
// Step 2: Trigger the Flow using FlowAPI (make sure to use the correct Flow Sys ID)
var flowSysId = '3Db39ed06c8316d6d0f9377d30feaad3c7'; // Replace with the actual Sys ID of your Flow
var result = sn_fd.FlowAPI.getRunner()
.subflow(flowSysId) // Specify the Flow Sys ID
.inForeground() // Run the flow synchronously
.withInputs(inputs) // Pass inputs to the Flow
.run();
// Step 3: Handle the result of the Flow execution
var outputs = result.getOutputs(); // Get the output of the Flow
// Check if the Flow execution was successful
if (result.success) {
gs.info("Flow Action executed successfully");
// Assuming the Flow returns the attachment Sys ID in the output
var attachmentSysId = outputs.attachmentSysId;
if (attachmentSysId) {
gs.info("Document attached with Sys ID: " + attachmentSysId);
data.flag = 0; // Document downloaded successfully
} else {
gs.error("No attachment returned from Flow action.");
data.flag = 1; // Error occurred, no attachment
}
} else {
// Flow execution failed
gs.error("Flow execution failed: " + result.errorMessage);
data.flag = 1; // Mark failure
}
} catch (ex) {
// Handle unexpected errors
var message = ex.message;
gs.error("Error in Flow execution: " + message);
data.flag = 1; // Mark failure
}
Widget client script:
$scope.downloadAndApprove = function(card_data) {
c.data.approval_sys = $scope.card_data.sys;
c.data.action = "downloadLink";
c.server.update().then(function() {
if (c.data.flag == 0) {
var downloadLink = card_data.doc_link; // Get the download link from card_data
window.open(downloadLink, '_blank');
} else {
gs.error('Download failed: No data returned.');
}
});
}
ng-template:
<a class="link-see-all" ng-href="#" ng-click="downloadAndApprove(card_data)" target="_blank">Link</a>