Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How can i read a csv file that is in a midserver folder via script.

bgmoreira
Tera Expert

 

Hello,

 

I need to create a script so that daily I read a csv file that another application is creating inside a folder on my midserver every day.

 

Can someone give me an example of how I can read this file via script and parse this csv?

 

exempla of csv file

 

 

4 REPLIES 4

palanikumar
Giga Sage
Giga Sage

This is not a recommended solution to import CSV from MID Server to Instance. 

There are options available. Refer "Attaching Files Directly using the Attachment API or MID Command Probes" from the below article:

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0817437

Thank you,
Palani

RafaeldaSP
Giga Expert

Hello @bgmoreira 

You can’t read a file directly from the MID Server folder with just a script in ServiceNow, because the instance has no direct access to the MID Server operating system.

The supported pattern is:

The external system drops the CSV into a known folder on the MID.

From ServiceNow, you trigger a probe or script through the ECC Queue to ask the MID to read the file.

The MID reads the file and sends the content back to the instance via ECC Queue.

Once you have the file content in the instance, you parse it with the sn_impex.CSVParser API.

Example of parsing once you already have the CSV content in a string:

var csv = "name,age,city\nJohn,30,NY\nJane,25,LA"; // file content received
var parser = new sn_impex.CSVParser();
var data = parser.parse(csv);

for (var i = 0; i < data.size(); i++) {
var row = data.get(i);
gs.info("Name: " + row.get("name") + " | Age: " + row.get("age") + " | City: " + row.get("city"));
}


To automate daily, you would:

Create a Scheduled Job to trigger the probe.

The probe reads the file on the MID.

The content comes back via ECC Queue.

Your script parses and loads the data into the target table.

This is the recommended flow: Instance ? MID Probe ? ECC Queue ? Parse in ServiceNow.

Bert_c1
Kilo Patron

Seems to be a question on a script to run on a Linux or Windows environment, to parse a csv file there.

You can use PowerShell commands on Windows. Refer the below URL:

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/import-csv?view=pow...

 

Thank you,
Palani