How to read file from mid server and create table row attachment

Michaela2
Kilo Contributor

Ok, currently I'm facing the following scenario:

For a ONE TIME migration (i.e. no schedules, no action triggers, etc)  I need to attach 20000 files to an already existing table.

Due to high security settings within the virtual environment it's impossible to use the Attachment REST API, so all files have been dumped on a mid server.

There's also a CSV that indicates which file gets assigned to which record (record_id;filename). I know how to create a datasource/table from this CSV, that's no problem.

I'm stuck, tho, on how to actually use that filename to access that file from, preferably, a freeform script. Something like this:

var gr_files = new GlideRecord ('u_my_files');
gr_files.query();
// loop files
while (gr_files.next()) {
  var gr = new GlideRecord ('u_my_table');
  gr.addQuery('record_id',gr_files.record_id);
  gr.query();
  if (gr.next()) {
    var sys_id = gr.sys_id;
    //HOW? READ CONTENT gr_files.filename FROM MID SERVER
    //HOW? CREATE attachment for sys_id
  }
}
 
3 REPLIES 3

Michaela2
Kilo Contributor

Sadly, that is exactly what I'm not looking for.

I'm not importing data.

All the data is already in the table.

The files are already on the Server.

The files are all kinds of types, like jpg, png, pdfs.... their content doesn't have to be analyzed in any way - just read from the server and written as an attachment to a record.

Is there any straightforward way to do this?

Thanks

Michaela2
Kilo Contributor

Well, since there obviously is no solution for this, I've resorted to a blatant hack.

I wrote a little .net app that now controls the ServiceNow page via the Browser's DOM document and clicks all the right buttons, reads the attachment list and feeds the missing filenames into the file picker.

Works like a charme.

Had to take care of a special charater bug tho. ServiceNow seems to insist on displaying filenames like "A & B.doc" as "A & B.doc".