Calling a REST API flow action into the server side/client side script

Pushpanjali3
Tera Contributor

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:

(function execute(inputs, outputs) {
    try {
        // Inputs
        var sysId = inputs.sysId;  // Record sys_id to attach the document to
        var taskId = inputs.taskId;  // Task ID to retrieve the document
        var userlogin = inputs.userlogin;  // User login to authenticate the request
        var documentName = "Document.pdf";  // Document name
        var contentType = "application/pdf";  // MIME type for PDF

        // Step 1: Remove existing attachments from the record
        var attachmentGR = new GlideRecord('sys_attachment');
        attachmentGR.addQuery('table_sys_id', sysId);
        attachmentGR.addQuery('table_name', 'x_stmin_generic_0_dms_generic_request');
        attachmentGR.query();
        while (attachmentGR.next()) {
            attachmentGR.deleteRecord(); // Remove any existing attachments
        }

        // Step 2: Call the REST API to download the document
        var request = new sn_ws.RESTMessageV2('x_stmin_generic_0.ST - DMS Approval', 'Get_task');
        request.setStringParameterNoEscape('userlogin', userlogin);
        request.setStringParameterNoEscape('taskId', taskId);

        // Step 3: Save the response body as an attachment
        request.saveResponseBodyAsAttachment('x_stmin_generic_0_dms_generic_request', sysId, documentName);

        // Execute the request
        var response = request.execute();
        var httpStatus = response.getStatusCode();
        var attachmentSysId = response.getResponseAttachmentSysid();

        // Step 4: Log the API call in the outbound table
        var grLogTable = new GlideRecord('u_it_integration_outbound_table');
        grLogTable.initialize();
        grLogTable.setValue('u_status', httpStatus);
        grLogTable.setValue('u_response_body', response.getBody());
        grLogTable.setValue('u_source', 'DMS Document Download');
        grLogTable.setValue('u_request_body', JSON.stringify({ taskId: taskId, userlogin: userlogin }));
        grLogTable.insert();

        // Step 5: Return the attachment sys_id as an output
        outputs.attachmentSysId = attachmentSysId;  // Return the Sys ID of the created attachment

    } catch (ex) {
        var message = ex.message;
        // Log any exception if it occurs
        var log = new sn_hr_integrations.IntegrationLogUtility().setOutboundLog(
            'DMS Document Download', 'Get Document', 'DMS', '', message, '', '', message,
            'Download document', '', 'REST Message-Document Download', taskId
        );
        outputs.message = message;  // Return the error message in case of failure
    }
})(inputs, outputs);

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>







 

0 REPLIES 0