Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

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

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @balaji_prusty1 

 

The issue is not with the code, the issue is with the username password as 4-1 errors are there.

 

A "401 Unauthorized" error in ServiceNow integrations indicates that the system is unable to access the requested data due to invalid authentication credentials, meaning the username and password used to connect to the external system are incorrect or lack the necessary permissions to access the specified data within ServiceNow. 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Debasis Pati
Tera Guru

Also Please check what type of authentication method you are using and if it is basic  check the user profile is having proper roles or not.

Regards,
Debasis

Hi Debasis,

 

Its not 401 error. Credential is working. We use the same credentials (Auth 2.0) for other updates. It's giving 400 error, whichs means the Json is not supporting. File error. 

 

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

@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

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}