Loading attachments to ServiceNow from a local machine via Batch Scripting

Migrating from another platform to ServiceNow can be challenging, especially when you need to import large amounts of historical data. One of the biggest challenges in this process is moving attachment files and keeping them with the proper record. Fortunately for us, ServiceNow provides an attachment API that can be leveraged in order to get our attached files back where they need to be. All you need is a unique identifier from your source records and a little magic concocted by the Pathways team.

The attached .cmd file can be used to load attachments from a local machine into ServiceNow via the Attachment API. This sample is targeting the incident table.

This is currently set to run through an Excel file (.csv) and match a sys_id from a record to another value from that record that acts as a unique identifier. This identifier in this case is looking for the folder name. The folder name matches to the sys_id column and all attachments from the folder are imported to the target record.

In this example, our folder structure is as follows: C:\Users\yourname\desktop\DataDump\SMAttachments\Attachments3\IM
IM is our “Incident Management” folder. Each folder within folder IM has the “record_id” as its folder name. The script will parse the columns in the spreadsheet and match column A (record_id) to column B (sys_id). These values will be input to the variables in the script which will fill in the values needed in the cURL message to determine your target.

Instructions to load:
1) Records must already be imported to ServiceNow prior to the attachment load

2) You’ll need to extract your records with the sys ID using export fields all URL where ‘table_name’ is the name of the table (https://instance.service-now.com/table_name.do?
(note: You can add additional parameters to filter e.g. &sysparm_query=u_record_id!=NULL)

3) Once you have the exported .csv file
a) Remove all columns except sys_id and your identifier column (record_id in this example)
b) Make sure that your identifier column is in column A and sys_id is in column B
c) Remove the header rows

You are now ready to add your inputs to the script. In our example, variable %%d refers to our identifier (folder name) and %%e is our target sys_id.

4) line 2 of the script looks for %%d in (filename.csv), where filename should be the name of your exported and edited list.

5) The other variables can be left alone. It will look for /D (directory) %%f in the current folder. It will then change directories to %%d (identifier/folder name) as it loops through the current folder. For each file %%g in the folder, the cURL script will call the attachments API and upload attachments, one at a time, to the target record.

6) In line 7, leave the variables in the URL as-is. The only thing you need to change here is the table name as this is not dynamic. In the attribute “-u”, you will need to enter a user id: password combination that has write access (preferably admin). The format is -u username:password. 

7) When your script is altered and ready, place it within the parent folder (IM in this example). You will also need to copy your Excel file here.

8) Hold shift and right-click and then select to open a command prompt in the current folder. Type in loadAttachmentsINC.cmd and press enter to begin the script.


Other notes – This process requires that you install cURL on your computer. You will also need to change your Windows environment variables to allow it to run (see attached instructions).