[Question] Upload attachment via script

kelvin_du
Giga Contributor

Hello all,

I have to write a script (perhaps it's a scheduled job) to upload the attachment from my computer.

For example:

There are 10 records in my Incident table (without attachment).

There are 10 folders in my computer, each folder contains files which I want to attach to a specific record. 10 folders correspond to 10 records.

Run the script to upload all the attachment to the records.

I do not have any idea to do this. It should be nice if you guys give me a hand on this.

Thanks a lot

9 REPLIES 9

adiddigi
Tera Guru

You cannot do this if it's in your computer unless your computer has FTP enabled / MID Server is installed on your computer and your computer has a Public IP address.



If let's say a MID Server is installed on your computer, move all the attachments into a single folder, then create a mapping between Incident Numbers and File names and write a MID Server Script Include Probe to read the files using a simple File reader and load them into Attachment table.



I know it sounds a lot, but let me know if you want to go this route and I will try getting you a working script.


Hi Abhi,



I have done the AttachmentCreator method Tim mentioned using Node.js running on a dedicated box for batch uploading decrypted files back up to an instance, worked great.



I have a new requirement where files will be dropped into a folder on a MID server and I need to be able to pick those files up and attach them to records.


We will have a mapping between file names and tables/records but having hard time finding the MID Server Script Include Probe and simple file reader you are mentioning.



Can you provide an example or links that will help me figure it out?



Thanks,



-e


Since it's already two days since you asked this, and though I don't have the code with me handy, I'll explain how I have done this in the past:




1. Have a Scheduled Job execute a function in a MID Server Script Include.


2. This MID Server Script Include would take the recordID, tableID and then POST that attachment to that record in ServiceNow.



3. For the MID Server Script Include to POST the attachment, we need to write a Processor in the system which is a webhook that can accept the POST from the MID Server Script include.



So that's three components there.



1. Writing a Scheduled Job is easy, all you have do is invoke a probe to call a MID Server Script Include.


Here is an excellent intro on how you can do that from John: JavascriptProbe and MID Server Script Includes | John Andersen




2. Writing that webhook which can accept the POST from the MID Server Script include which will read the files from the MID Server. This is something I have written multiple times and here is a stripped down standard version of a Script Include that I have composed:


ServiceNow_AttachmentWebhook.js · GitHub




3. And finally that Script Include that reads the files from MID Server. Here is another Github link for an application for Port, which does the exact same thing.




In there, there is a Script Include called "PT_FileImporter.js" which you should read to understand how this can be done.



SNPort/PT_FileImporter.js at master · abhididdigi/SNPort · GitHub




Shoot me up with questions if you have any.


Wow, thanks so much Abhi.... this is great!!!