The CreatorCon Call for Content is officially open! Get started here.

Unable to retrieve comments from sys approval table in the script include in outbound integration.

Pushpanjali3
Tera Contributor

Script include:

var Update_DMS_Task_Script = Class.create();
Update_DMS_Task_Script.prototype = {
    initialize: function() {},



    callAlert: function(sysId,ipState) {
        //x_stmin_generic_0_dms_generic_request_list

        var comment = '',
            getId, loginName, payload, document_id, approver, state, flag = 0;
        //get Doc ID
        var gr_ap = new GlideRecord('sysapproval_approver');
        gr_ap.addQuery('sys_id', sysId);
        gr_ap.query();
        if (gr_ap.next()) {
            document_id = gr_ap.document_id;
            approver = gr_ap.approver;
            state = gr_ap.state;
        }
        // Retrieve the task ID from the document ID
        var grDms = new GlideRecord('x_stmin_generic_0_dms_generic_request');
        grDms.addQuery('sys_id', document_id);
        grDms.query();

        if (grDms.next()) {
            getId = grDms.object_id;
            gs.info('Retrieved Task ID: ' + getId);
        } else {
            gs.error('No matching record found in x_stmin_generic_0_dms_generic_request for document_id: ' + current.document_id);
        }

        // Retrieve the user login name
        var userDms = new GlideRecord('sys_user');
        userDms.addQuery('sys_id', approver);
        userDms.query();

        if (userDms.next()) {
            loginName = userDms.u_login;
            gs.info('Retrieved Login Name: ' + loginName);
        } else {
            gs.error('No matching user found in sys_user for approver: ' + approver);
        }
        // Log the current state and approver for debugging
        gs.info('Current state: ' + getId + loginName + state);
        gs.info('Current approver: ' + approver);
        gs.info('Current document_id: ' + document_id);
       // if (state == 'approved' || state == 'requested') {
        if(ipState == 'approved')
        {
            payload = {
                "userId": loginName.toString(),
                "taskId": getId.toString(),
                "action""Approved"

            };


        }

        //if (current.state == 'rejected') {
            if(ipState == 'rejected'){
            // Query the sysapproval_approver table to retrieve the comment
            var grApproval = new GlideRecord('sysapproval_approver');
            grApproval.addQuery('document_id', document_id);
            grApproval.addQuery('approver', approver);
            grApproval.query();

            if (grApproval.next()) {
                // Retrieve the comments using the JournalEntry API
                comment = grApproval.comments.getJournalEntry(1);
               // var cleanedComment = comment.replace(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} - [\w\s]+\s\(\w+\)\n/, '').trim();

                //comment = cleanedComment;

            } else {
                gs.error('No record found in sysapproval_approver for sysapproval: ' + document_id + ' and approver: ' + approver);
            }

            // Add rejectReason to the payload

            payload = {
                "userId": loginName.toString(),
                "taskId": getId.toString(),
                "action""Rejected",
                "rejectReason": comment
            };
        }
        var payloadJson = JSON.stringify(payload);

        gs.info('Payload to be sent: ' + payloadJson);
        var requestBodyString = payloadJson;
        var responseBody, httpStatus = '';
        try {
            var r = new sn_ws.RESTMessageV2('x_stmin_generic_0.ST - DMS Approval''Update_task');

            r.setRequestBody(payloadJson);

            // Send the request and get the response
            var response = r.execute();
            responseBody = response.getBody();
            httpStatus = response.getStatusCode();

            // Log the response for debugging
            gs.info('DMS Task Status Update Response: ' + responseBody);
            gs.info('HTTP Status: ' + httpStatus);
            // Log the results to the outbound log table
            var grLogTable = new GlideRecord('u_it_integration_outbound_table');
            grLogTable.initialize();
            grLogTable.setValue('u_status', httpStatus);
            // grLogTable.setValue('u_action', current.state ? current.state.toString() : '');

            grLogTable.setValue('u_response_body'JSON.stringify(responseBody));

            grLogTable.setValue('u_source''DMS');
            grLogTable.setValue('u_request_body', requestBodyString);
            grLogTable.insert();


            // Handle the response based on HTTP status
            if (httpStatus >= 200 && httpStatus < 300) {
                flag = 0;
                gs.info('DMS Task Status successfully updated.');
                //return flag;
            } else {
                flag = 1;
                // Error handling for non-200 responses

                /* var errorMessage = 'An error or a timeout occurred while trying to perform this task. ' +
                     'Please retry in a while. If the error persists, please check that the task is still pending ' +
                     'for your action in DMS and consider to approve or reject directly in DMS, else get assistance from DMS support.';


                 gs.error(errorMessage);
                 gs.addErrorMessage(errorMessage); // Display error message to the user



                 gs.info("DMS DMS " + current.sys_id);*/

                var gr_ap1 = new GlideRecord('sysapproval_approver');
                gr_ap.addQuery('sys_id', sysId);
                gr_ap1.query();
                if (gr_ap1.next()) {
                    gr_ap1.setValue('state''requested');
                    //              current.state = 'Requested';
                    gr_ap1.update();
                }


                gs.error('Failed to update DMS Task Status. HTTP Status: ' + httpStatus);

                // return flag;

            }
            gs.info("Return value in SI" + flag);
            return flag;




        } catch (ex) {
            var grLogTable1 = new GlideRecord('u_it_integration_outbound_table');
            grLogTable1.initialize();
            grLogTable1.setValue('u_response_body', ex.message);
            grLogTable1.setValue('u_source''DMS exceptions');
            grLogTable1.setValue('u_status', httpStatus);
            grLogTable1.setValue('u_response_body'JSON.stringify(responseBody));
            grLogTable1.setValue('u_error_message', ex.message);
            grLogTable1.insert();


        }


    },

    /*callAlert:function(){
    var getData = getParameter('sysparam_value');
    gs.info('check alert' + getData);
    var gr_ap= new GlideRecord('sysapproval_approver');
            gr_ap.addQuery('sys_id', getData);
            gr_ap.query();
            if (gr_ap.next()) {
                 var status = gr_ap.state;
            }

            if (status == 'requested')
            {

                return true;
            }
 },*/

    type: 'Update_DMS_Task_Script'
};




widget : server script
 if (input.action == "setApprove") {
gs.info("DMS State approval"+input.state);
 
 
        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()) {
 
if (approval_gr.document_id.sys_class_name != 'x_stmin_generic_0_dms_generic_request') {
 
            
            approval_gr.state = input.state;
            if (input.state == "rejected") {
                approval_gr.comments = input.comment;
            }
            approval_gr.update();
}  
else {
var getTaskID = approval_gr.getDisplayValue('document_id');
var call_rest = new x_stmin_generic_0.Update_DMS_Task_Script();
var get_msg = call_rest.callAlert(input.approval_sys,input.state);
gs.info("Return Value DMS"+get_msg.toString()+JSON.stringify(get_msg)+JSON.parse(get_msg)+get_msg);
 
if(get_msg == 0)
{
approval_gr.state = input.state;
            if (input.state == "rejected") {
                approval_gr.comments = input.comment;
            }
            approval_gr.update();
 
}
else if(get_msg == 1)
 
{
gs.info("DMS_errorMessage"+errorMessage);
var errorMessage = 'A technical error occurred while trying to perform this task. Please retry in a while.'+'<br>'+'If the error persists, please try to approve or reject the task in DMS, else get assistance from DMS support (Helix Ticket)';
gs.info("DMS_errorMessage"+errorMessage);
 
 
 
                // gs.error(errorMessage);
                 gs.addErrorMessage(errorMessage); // Display error message to the user
}
 
 
}
1 REPLY 1

Mark Roethof
Tera Patron
Tera Patron