Send Attachment from One ServiceNow to Other ServiecNow Instance

balaji_prusty1
Giga Guru

Hi,

I am configuring sending attachments from Incident (ServiceNow) to Other ServiceNow instances using the custom app. I configured the below script in BR. But it is always failing with 401 errors. (As below)

{"error":{"message":"Invalid file type: application/json","detail":null},"status":"failure"}

 

------------------------BR Script-------------------

var qry = 'table_name=incident^u_sent_flag=false^table_sys_id=' + current.sys_id;
 var att = new GlideRecord("sys_attachment");
    att.addEncodedQuery(qry);
    att.query();
    while (att.next()) {
        try {

            var r = new sn_ws.RESTMessageV2('x_mea_inte_Integration', 'Send Attachment');
            r.setStringParameterNoEscape('table_sys_id', '611b0822fbb392103bf0f471beefdc06'); // sys_id hard coded  to test
            r.setStringParameterNoEscape('table_name', 'incident');
            r.setStringParameterNoEscape('file_name', att.file_name);
            r.setStringParameterNoEscape("Accept", "application/json");
            r.setStringParameterNoEscape("Content_type", att.content_type);
            r.setRequestBodyFromAttachment(att.sys_id);
            var response = r.execute();
            var responseBody = response.getBody();
            var httpStatus = response.getStatusCode();
            gs.info("Attachment Update Responce Body : " + responseBody);
            gs.info("Attachment update status : " + httpStatus);
        } catch (ex) {
            var message = ex.message;
        }

    }
----------------------
 
Can someone please help with this?
 
Thanks in Advance.
2 ACCEPTED SOLUTIONS

@balaji_prusty1 

did you check the links I shared yesterday?

Unable to send attachment via REST 

Business rule to send attachment to 3rd Party Tool via REST 

Send attachment via rest message 

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

balaji_prusty1
Giga Guru

Thanks All,

 

We are missing adding these parameters in the rest endpoint. (highlighted in red). Only sent over BR but missed adding in the end point. It's working now.

 

https://******.service-now.com/api/now/attachment/file?table_name=${table_name}&table_sys_id=${table_sys_id}&file_name=${file_name}

View solution in original post

14 REPLIES 14

Mark Manders
Mega Patron

Try this as your script, but also check the authentication. 401 normally means that your authentication is incorrect.

var qry = 'table_name=incident^u_sent_flag=false^table_sys_id=' + current.sys_id;
var att = new GlideRecord("sys_attachment");
att.addEncodedQuery(qry);
att.query();

while (att.next()) {
    try {
        var r = new sn_ws.RESTMessageV2('x_mea_inte_Integration', 'Send Attachment');
        
        // Ensure authentication is set up in the RESTMessageV2 record
        r.setStringParameterNoEscape('table_sys_id', current.sys_id);
        r.setStringParameterNoEscape('table_name', 'incident');
        r.setStringParameterNoEscape('file_name', att.file_name);

        // Explicitly setting headers
        r.setRequestHeader("Accept", "application/json");
        r.setRequestHeader("Content-Type", "multipart/form-data"); // Correct MIME type for file upload
        
        // Attach the file
        r.setRequestBodyFromAttachment(att.sys_id);

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

        gs.info("Attachment Update Response Body: " + responseBody);
        gs.info("Attachment update status: " + httpStatus);
    } catch (ex) {
        gs.error("Error sending attachment: " + ex.message);
    }
}

 


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Thanks for the Quick response. Getting below error. I am trying to attach a jpeg file.

 

: {"error":{"message":"Invalid file type: image/jpeg","detail":null},"status":"failure"}

Debasis Pati
Tera Guru

Hello @balaji_prusty1 ,

var qry = 'table_name=incident^u_sent_flag=false^table_sys_id=' + current.sys_id;
var att = new GlideRecord("sys_attachment");
att.addEncodedQuery(qry);
att.query();

while (att.next()) {
try {
var r = new sn_ws.RESTMessageV2('x_mea_inte_Integration', 'Send Attachment');

// Set the required parameters
r.setStringParameterNoEscape('table_sys_id', '611b0822fbb392103bf0f471beefdc06'); // sys_id hardcoded to test
r.setStringParameterNoEscape('table_name', 'incident');
r.setStringParameterNoEscape('file_name', att.file_name);

// Set the request headers correctly
r.setRequestHeader("Accept", "application/json");
r.setRequestHeader("Content-Type", att.content_type); // Use the correct content type

// Attach the request body from the attachment
r.setRequestBodyFromAttachment(att.sys_id);

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

gs.info("Attachment Update Response Body : " + responseBody);
gs.info("Attachment update status : " + httpStatus);
} catch (ex) {
var message = ex.message;
gs.error("Error sending attachment: " + message);
}
}

Please try the above script 

If my response helps you please mark it as helpful/correct.

Thanks In advance.

Regards,
Debasis

Thanks for the Quick response. Getting below error. I am trying to attach jpeg file.

 

: {"error":{"message":"Invalid file type: image/jpeg","detail":null},"status":"failure"}