
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 05-09-2023 04:27 AM
Hello,
In this article, I will show you steps on how you can integration ServiceNow with Dropbox API with simple REST API.
I am querying the incident table and fetching all reocrds which are created in the last week and transferring the attachments of those incidents to Dropbox.
1. Get the dropbox API details from here
2. To upload file into dropbox, check out dropbox API related to file upload
3. To get the credentials/token and sample request format, access the link
4. Once you have the details, then let us write a simple script in servicenow to read the file from ServiceNow and send to dropbox.
5. Create a custom application in ServiceNow. Then go to Script includes and create new and add below code.
var dropboxUtils = Class.create();
dropboxUtils.ENDPOINT = "https://content.dropboxapi.com//2/files/upload";
dropboxUtils.TOKEN = "Bearer sl.BeDOOeyCVbmXDsdtzKYF3pXL-yv-8ZUMmfSLsiGBZkSg0CAhOJIpBzpZeWIuR-GPZPZZHQ8Cwmbbqj3YO7ZJ8TnT_muSsUSO8jR7SIKqcDarC_MTkgyJUnH7-4l_VRii9CsB8QUX"; //your dropbox authorization token
dropboxUtils.prototype = {
initialize: function() {},
upload_file: function(number,att) {
var request = new sn_ws.RESTMessageV2();
request.setEndpoint(dropboxUtils.ENDPOINT);
request.setHttpMethod('POST');
request.setRequestHeader("Accept", "application/octet-stream");
request.setRequestHeader("Authorization", dropboxUtils.TOKEN);
request.setRequestHeader('Content-Type', 'application/octet-stream');
request.setRequestHeader('Dropbox-API-Arg', '{"path":"/' + number+"-"+att.file_name + '","strict_conflict":true,"mode":{".tag":"overwrite"}}');
//read the content of the attachment to pass to Dropbox API.
var sa = new GlideSysAttachment();
var content64 = sa.getContent(att);
request.setRequestBody(content64); //content of the file.
var response = request.execute();
gs.info(response.getBody());
},
type: 'dropboxUtils'
};
6. Then create a fix script. You can also write a scheduled job and can make this run once every week.
//fetch all my tickets created in last week and upload their attachments to dropbox.
var grInc = new GlideRecord("incident"); //you can use any table and records whose attachments you want to transfer
grInc.addEncodedQuery("sys_created_onONLast week@javascript:gs.beginningOfLastWeek()@javascript:gs.endOfLastWeek()");
grInc.query();
gs.info(grInc.getRowCount());
while (grInc.next()) {
//Read the attachments of the record which you want to transfer.
var att = new GlideRecord('sys_attachment');
att.addQuery("table_sys_id", grInc.sys_id);
att.query();
while (att.next()) {
new dropboxUtils().upload_file(grInc.number.toString(), att);
}
}
7. Run the fix script. Once the script is ran, go to your dropbox folder and check for the files.
Here is the sample output. I had 2 incidents created in last week and each one has 2 attachments.
Let me know if you have any questions in the comments below.
You can also check my other articles on integrations here.
Integration with 3rd Party API using Flow designer
Delete Multiple Records using Rest API
Mark the article as helpful and bookmark if you found it useful.
- 794 Views