Help on Script to send attachment through REST as multipart/form data
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2023 06:01 AM
Hello All,
Can anyone tell me step script to send an attachment as multipart/form data via REST ??
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2023 11:00 PM
are you not able to reach to that endpoint directly?
The above links should help you in writing the script/logic in both the cases i.e. with & without mid server
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-20-2023 05:44 AM
function sendAttachmentAsMultipartFormData(current) {
var grAttachment = new GlideRecord('sys_attachment');
grAttachment.addQuery('table_name', 'sc_req_item');
grAttachment.addQuery('table_sys_id', current.sys_id);
grAttachment.query();
while (grAttachment.next()) {
var url = '<YOUR API ENDPOINT URL>'; // Replace with your API endpoint URL
var body = new GlideHTTPRequest(url);
var multipart = new GlideMultipartFormData();
multipart.addFile('attachment', grAttachment.getValue('file_name'), grAttachment.getValue('file_stream'));
body.setMethod('POST');
body.setMultipartData(multipart);
body.addHeader('Content-Type', 'multipart/form-data');
var response = body.post();
gs.info('Attachment sent with response: ' + response.getBody());
}
}
In this script, we first query for all attachments that are associated with the RITM using a GlideRecord query. We then iterate over each attachment and create a new instance of the GlideHTTPRequest class to send the attachment in Multi/Part form data.
To create the Multi/Part form data, we create a new instance of the GlideMultipartFormData class and add the attachment file using the addFile method. We then set the Multi/Part form data in the GlideHTTPRequest instance using the setMultipartData method and add a header for the Content-Type using the addHeader method.
Finally, we send the HTTP request using the post method and log the response.
Note that you will need to replace the <YOUR API ENDPOINT URL> placeholder in the script with the URL of your API endpoint that accepts Multi/Part form data.
You can call this function from a Business Rule on the RITM table, for example, in the onAfter script section for the insert or update events. Alternatively, you can create a Script Include and call the function from a Scheduled Job or a Workflow.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2023 05:06 AM
@Shiva41 i have to use mid server in between while sending the attachment so what changes i need to make in the code ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2023 01:24 PM
Hi
To send an attachment as multipart/form data via the REST in ServiceNow:
Create an attachment object and set the file name and content, then create a REST message object and set the endpoint to 'rest_attachment', method to 'post', request body to the attachment object, and content type to 'multipart/form-data'. Now send the REST message object using 'execute'.
Please consider using Exalate integration software. It synchronizes data between ServiceNow and other systems, including attachments, using a customizable solution. It will make this way easier 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2023 12:52 AM
@Mathieu Lepoutr Thanks for the response but i cant user exalate integration software. Can you please help me on some sets of line of code that are been used on the logic you have explained above, so that i can take the reference and then i will be able to execute it