Wednesday, June 2, 2021

Parse attached CSV file in ServiceNow using script in UI Action

The following is a sample script to parse a CSV file in UI Action.


var gr = new GlideRecord('sys_attachment');

gr.addQuery("table_name", "incident");

gr.addQuery("table_sys_id", current.sys_id); // sys_id of attachment record

gr.query();


if (gr.next()) {

    var gsa = new GlideSysAttachment();

    var bytesInFile = gsa.getBytes('incident', current.sys_id);

    var fileContent = Packages.java.lang.String(bytesInFile); 

    gs.log("Contents of CSV:  " + fileContent);

    fileContent = String(fileContent);

    var delimiter = ',';

    var quoteChar = '"';


    var x = new sn_impex.CSVParser().parseLineToArray(fileContent, delimiter, quoteChar);

gs.log("line 0 content" + x[0]);

}