Attachment API - Attach a file by URL

Andr_1
Tera Contributor

Hello,
I am testing the Attachment API (Upload an attachment from binary request (POST) with the "Rest API Explorer" module to see if it works correctly for my needs.

Andr_1_0-1693386761781.png

 
I uploaded a file from my computer (local) and when I try to send, it works well.

Now my question is How can I attach a file from a URL instead of uploading it locally? I have a URL with a PDF file and I want to attach this file to the HR Case form.

 

Thank you

 

1 ACCEPTED SOLUTION

Mohith Devatte
Tera Sage
Tera Sage

Hello @Andr_1 ,

I think you might have to do it via script using RESTMESSAGEV2 API like below 

 


        var attachmentMessage = new sn_ws.RESTMessageV2();

        try {
            attachmentMessage.setHttpMethod("get");
            attachmentMessage.setEndpoint(targetInstanceURL);
            attachmentMessage.saveResponseBodyAsAttachment("incident", {record sys_id}, {filename});
            var response = attachmentMessage.execute();
            var httpStatus = response.getStatusCode();
        } catch(ex) {
            gs.info("ERROR (sendAttachments): " + ex);
        }
    }

 

Note :This is untested , but try giving it a shot .If it works let me know

Replace targetedInstanceURL with your Teams link

 

If this helps 

Mark the  answer correct 

Thanks

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Andr_1 

it depends on which application is consuming the endpoint

What's your business use-case here?

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

@Ankur Bawiskar 

thank you for your feedback.

I think that I did not get your question.

I am building a integration with Microsoft Team, and I want to attach a file from Teams to the HR Case form, but the file is in URL format.

I just want to know how can I import the URL file in the Attachment API instead upload directly the file from my computer.

@Andr_1 

with just file url you cannot attach file.

you need to fetch the file using API and then attach it to record

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

Mohith Devatte
Tera Sage
Tera Sage

Hello @Andr_1 ,

I think you might have to do it via script using RESTMESSAGEV2 API like below 

 


        var attachmentMessage = new sn_ws.RESTMessageV2();

        try {
            attachmentMessage.setHttpMethod("get");
            attachmentMessage.setEndpoint(targetInstanceURL);
            attachmentMessage.saveResponseBodyAsAttachment("incident", {record sys_id}, {filename});
            var response = attachmentMessage.execute();
            var httpStatus = response.getStatusCode();
        } catch(ex) {
            gs.info("ERROR (sendAttachments): " + ex);
        }
    }

 

Note :This is untested , but try giving it a shot .If it works let me know

Replace targetedInstanceURL with your Teams link

 

If this helps 

Mark the  answer correct 

Thanks