Loading attachments to ServiceNow from a local machine via Batch Scripting in Windows

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, ServiceNow provides an attachment REST API that can be leveraged to move our attached files back where they need to be. All you need is a unique identifier from your source records on your target table 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. 

The batch file is written to use a .csv file that you create from your target table to match up each ServiceNow record sys_id with another field on the record that acts as a unique identifier for your legacy system. If it then finds a folder that matches the legacy identifier, it will upload any files in that folder as attachments to the target record. 

All you need to do is create a source CSV file and plug in some variables!

For each step I will provide an example. In the example use case, we are importing attachments from an imaginary system called HelpDesk. In ServiceNow, we’ve created a custom field called u_helpdesk_id, and when we imported our legacy data, we populated that field with the unique identifier from that system. 

Pre-Requisites:

  1. Records must already be imported to ServiceNow prior to the attachment load
  2. Records must have a field that holds the unique identifier from the legacy system
  3. Attachments must be in a folder structure where each folder name matches the legacy system’s unique identifier of the attachments that it holds
  4. Access to a username and password of a ServiceNow account that can write to the table needed (preferably admin)
  5. cURL installed on your local computer (often installed by default in modern Windows)

Example:

Here is an example of how your folders should be set up. Each folder is named for the legacy unique identifier, and inside of each folder are the attachments for that record.

 

Instructions to Load Attachments:

Step 1: Extract a CSV of your records from ServiceNow

You’ll need to extract your records with the sys ID using the “export fields all” URL. Replace the text bracketed by <> with your instance, table and field that holds the legacy unique identifier.

https://<your-instance>.service-now.com/<table>_list.do?sysparm_query=<legacy_id_field>ISNOTEMPTY&CSV&sysparm_default_export_fields=all

Paste this into your browser bar, and it will automatically download the records with all fields, including the sys_id, to your default download location.

Example: 

In our example, our target table is Incident, and our custom legacy unique identifier field is u_helpdesk_id, so our url will be this:

https://demoinstance.service-now.com/incident_list.do?sysparm_query=u_helpdesk_id ISNOTEMPTY&CSV&sysparm_default_export_fields=all

Step 2: Format CSV file

Once you have located the exported .csv file, open it in Excel or your favorite editor.

  1. Remove all columns except sys_id and your identifier column (u_helpdesk_id in this example)
  2. Make sure that sys_ids are in column A and your legacy identifier is in column B
  3. Remove the header rows
  4. Save (make sure to keep in csv format) in a convenient location such as C:\temp\import. Note: make sure there are no spaces in your path.
  5. Make sure to close out of the file!

You are now ready to add your inputs to the script!

Example:

We’ve downloaded our csv and edited as above, so this is what it looks like now:

Step 3: Download and edit the batch file

Click here to download the batch file <link loadAttachmentsToServiceNow.cmd>.

After unzipping, right-click on the .cmd file and select “Edit”

At the top of the file you will see a section called “EDIT VARIABLES HERE”. Edit the variables as needed. For example: 

set instance=demoinstance

set targetTable=incident

set rootFolder=C:\temp\import

set pathCSV=C:\temp\exported_file.csv

set LOGFILE=C:\temp\import_log.txt

set username=admin

set password=admin

Keeping the recommended paths and file names will give you the best results!

Note: If your paths above have spaces in them, move your files to a location without spaces, or the script will fail!

Make sure to save the file once you are done editing!

Step 4: Test

Don’t skip this step! 

Once we let this script rip, it will start uploading to records. You want to first make sure that it’s behaving correctly before you let it loose on your instance. And as always, test first in a sub-prod instance!

The .cmd file as downloaded has the command that uploads the files commented out. Check that this is the case by searching for “curl” in the file. You should see that it is commented out with the keyword “rem”.

Locate the .cmd file in the explorer. Double-click the file to run it. Check the location of the log file and validate that the script is matching up your records properly. Edit your .cmd file and your import csv and rerun as many times as you need to be sure that everything is matching up correctly. 

Example: 

Here you can see some examples from our sample data. We can check these against the sys_ids of the records in ServiceNow, to make sure that everything matches up correctly.

Step 5: Upload those attachments!

At this point, you’ve tested thoroughly and you’re ready to reap the rewards!

Edit the script to remove the “rem” in front of that curl command.

It should now look like this:

Save the .cmd file, then double-click on the file to run it. You should be able to watch as the attachments get uploaded. As it goes, it will log the API responses to your log file. 

Note: If you get the error message “The system cannot find the file <path>.csv.” but the path looks correct, make sure you don’t have the file still open in Excel!

Example: 

We’ve run the file and watched our uploads, and now it’s complete!

Step 6: Validate

First, make sure to check your log file to see the behind-the-scenes action.  Here you will see any errors that occurred. You may need to go back and edit your variables if you encounter problems. In the example below, you can see that there was an issue with the username/password

Example: 

In this example, you can see that the username/password combination didn’t work! 

You can validate the uploads by going to your target table and opening your records. You can also check the [sys_attachment] table to see the attachments appear as they come in. Filter by the target table field to see the ones you are looking for!

We hope this has helped make the migration process a little smoother! 

While the above technical tip has been provided with care and consideration, it’s important to acknowledge that individual circumstances may vary. Always ensure compatibility and feasibility within your specific ServiceNow environment before implementing any suggestions. Additionally, back up your data and proceed with caution when making any changes to your instance or workflows. 

As with any change in ServiceNow, make sure you test any changes prior to moving to production.