Send Attachment one instance to another

nowitsvashu
Tera Guru

Hi there I'm getting error as Script: Error parsing JSON response: Unexpected token:

Help me to fix it

 

 

(function executeRule(current, previous /*null when async*/) {

    var targetInstanceURL = 'https://dev185360.service-now.com/';
    var targetUserID = 'admin';
    var password = ''; //Hide while posting question
    var text = "";
    var attachmentMsg = "";

    try {
        // Create REST Message to create a record
        var r = new sn_ws.RESTMessageV2("E-Bonding dev185360", "Create Record");
        r.setStringParameter("sys_id", current.sys_id);
        r.setStringParameter("category", current.category);
        r.setStringParameter("short_description", current.short_description);
        r.setStringParameter("caller_id", current.caller_id);

        var response = r.execute();
        var responseBody = response.getBody();
        var httpResponse = response.getStatusCode();

        gs.log("HTTP Response: " + httpResponse);
        gs.log("Response Body: " + responseBody);

        if (httpResponse == 201 || httpResponse == 200) {
            text = "Incident request processed successfully.";
            
            var parsed;
            try {
                parsed = JSON.parse(responseBody);
            } catch (jsonError) {
                gs.error("Error parsing JSON response: " + jsonError.message);
                text = "Incident creation failed. Invalid JSON response.";
                gs.addErrorMessage(text);
                return;
            }

            if (parsed.result && parsed.result.sys_id) {
                var targetRecordSysId = parsed.result.sys_id;
                text = "Incident Created Successfully. Sys ID: " + targetRecordSysId;

                var attachmentCount = sendAttachments(current.getTableName(), current.sys_id, targetRecordSysId);

                if (attachmentCount != 'none') {
                    attachmentMsg = " Attachments Successfully Sent: " + attachmentCount + " attachments.";
                } else {
                    attachmentMsg = " No attachments found.";
                }
            } else {
                text = "Incident creation response received, but sys_id is missing.";
                gs.error(text);
            }

        } else {
            text = "Incident not created. HTTP Response: " + httpResponse;
            gs.error("Incident creation failed. Response Body: " + responseBody);
        }

    } catch (e) {
        gs.error("Error in REST API call: " + e.message);
        text = "Incident creation failed. Check error logs.";
    }

    text = text + " " + attachmentMsg;
    gs.addInfoMessage(text);

    // Function to send attachments
    function sendAttachments(sourceTable, sourceID, targetID) {
        var attachmentRecord = new GlideRecord("sys_attachment");
        attachmentRecord.addQuery("table_sys_id", sourceID);
        attachmentRecord.addQuery("table_name", sourceTable);
        attachmentRecord.query();

        var attachmentCount = 0;

        try {
            while (attachmentRecord.next()) {
                gs.log("Processing attachment: " + attachmentRecord.file_name);

                var aMsg = new sn_ws.RESTMessageV2(); // Fixed this
                aMsg.setHttpMethod("post");
                aMsg.setBasicAuth(targetUserID, password);
                aMsg.setEndpoint(targetInstanceURL + 'api/now/attachment/file');
                aMsg.setQueryParameter("table_name", sourceTable);
                aMsg.setQueryParameter('table_sys_id', targetID);
                aMsg.setQueryParameter("file_name", attachmentRecord.file_name);
                aMsg.setQueryParameter("content_type", attachmentRecord.content_type);
                aMsg.setRequestBodyFromAttachment(attachmentRecord.sys_id); // Set request body from attachment

                var res = aMsg.execute();
                var resBody = res.getBody();
                var resStatus = res.getStatusCode();

                gs.log("Attachment response status: " + resStatus);
                gs.log("Attachment response body: " + resBody);

                if (resStatus == 201) {
                    attachmentCount++; // Increment count if successful
                } else {
                    gs.error("Failed to send attachment: " + resBody);
                }
            } 
        } catch (e) {
            gs.error("Error while sending attachments: " + e.message);
        }

        return attachmentCount > 0 ? attachmentCount : 'none'; // Return count or 'none'
    }

})(current, previous);

 

 

 

4 REPLIES 4

Community Alums
Not applicable

Hi @nowitsvashu ,

do you've any special charecters in variables that you're setting ?

reffering this part:

 var r = new sn_ws.RESTMessageV2("E-Bonding dev185360", "Create Record");
        r.setStringParameter("sys_id", current.sys_id);
        r.setStringParameter("category", current.category);
        r.setStringParameter("short_description", current.short_description);
        r.setStringParameter("caller_id", current.caller_id);

 

nowitsvashu_0-1726995293486.png

 

Community Alums
Not applicable

@nowitsvashu ,

not this, 

The values that you're passing in sys_id, sd, cat,caller id.

do you have any special characters?

Mani A
Tera Guru

Did you check the logs for responseBody..at which step you are getting this error?