Certain variables are not being assigned in output Array.Object in flow designer action

Colin Wilson
Giga Guru

On Xanadu - glide-xanadu-07-02-2024__patch5-12-24-2024.

 

I am building an action that will parse through a .csv in the sys_attachment table, and make the data accessible within a flow. 
After ascertaining the .csv, parsing the data with sn_impex.CSVParser, and trying to assign it to a defined output of Array.Object, all of the values in the first column of the CSV file are set to null. 

 

An Example: 

For this data:

Screenshot 2025-02-20 at 1.47.09 PM.png

 

Action: 

Inputs: 

  • Attachment Table SysID (Sys ID (GUID))
  • Attachment Table Name (string)
  • Delimiter (string)
  • Quote Character (string)

Screenshot 2025-02-20 at 1.58.43 PM.png

Script Step:

Inputs: 

Screenshot 2025-02-20 at 2.08.22 PM.png

Outputs:

Screenshot 2025-02-20 at 2.09.30 PM.png

Script:

(function execute(inputs, outputs) {
var a_table_name_sys_id = inputs.attachment_sysid;
var a_table_name = inputs.attachment_table_name;
var a_delimiter = inputs.delimiter; // default is ,
var a_quoteCharacter = inputs.quote_character; // default is "

var gsa = new GlideSysAttachment();
var bytesInFile = gsa.getBytes(a_table_name,a_table_name_sys_id);
var originalContentsInFile = Packages.java.lang.String(bytesInFile);
originalContentsInFile = String(originalContentsInFile);
var fileData = originalContentsInFile.split('\n');
var csvheaders = sn_impex.CSVParser().parseLineToArray(fileData,a_delimiter,a_quoteCharacter);
var headers = cleanHeaders(csvheaders); //replace all spaces in csv headers with underscores. 
var i = 0;
var csvArray = []; 

fileData.forEach(function(line){
    try{
        var lineObj = new sn_impex.CSVParser().parseLineToObject(line,headers,a_delimiter,a_quoteCharacter);
        //gs.info(JSON.stringify(lineObj,'',' '));
        csvArray[i] = lineObj;
        i++;
    }
    catch(e){
        gs.error("in catch: " + e);
    }

});

//gs.info('csvArray: ' + JSON.stringify(csvArray,'',' '));
csvArray.shift();// remove the header object from the csvArray
outputs.users = csvArray;

function cleanHeaders(h){
    var ret = [];
    for(var i=0;i<h.length;i++){
        ret.push(h[i].replace(/ /g,"_"));
    }
    return ret;
}

})(inputs, outputs);

 

 

If you un-comment the gs.info's on lines 20 & 30, you'll notice that the data is all there. The assignment operator on line 32 does not seem to respect whatever column was originally in the first column of the CSV. 

 

Here is the output from testing the action: 
Screenshot 2025-02-20 at 2.12.59 PM.png.

 

 

Running a second test with the following data: 

Screenshot 2025-02-20 at 2.16.25 PM.png

results in:

Screenshot 2025-02-20 at 2.19.23 PM.png

 

Any thoughts as to why there is a null value appearing from nowhere?

Sample CSV files attached

 

 

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@Colin Wilson 

your file columns are not in sequence in both the files.

Is that done purposely?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

that is done purposely. order of file columns should not matter. 

Ankur Bawiskar
Tera Patron
Tera Patron

@Colin Wilson 

try this

(function execute(inputs, outputs) {
    var a_table_name_sys_id = inputs.attachment_sysid;
    var a_table_name = inputs.attachment_table_name;
    var a_delimiter = inputs.delimiter; // default is ,
    var a_quoteCharacter = inputs.quote_character; // default is "

    var gsa = new GlideSysAttachment();
    var bytesInFile = gsa.getBytes(a_table_name, a_table_name_sys_id);
    var originalContentsInFile = Packages.java.lang.String(bytesInFile);
    originalContentsInFile = String(originalContentsInFile);
    var fileData = originalContentsInFile.split('\n');
    var csvheaders = sn_impex.CSVParser().parseLineToArray(fileData[0], a_delimiter, a_quoteCharacter);
    var headers = cleanHeaders(csvheaders); // replace all spaces in csv headers with underscores
    var i = 0;
    var csvArray = [];

    fileData.forEach(function(line, index) {
        if (index === 0) return; // Skip the header line
        try {
            var lineObj = new sn_impex.CSVParser().parseLineToObject(line, headers, a_delimiter, a_quoteCharacter);
            gs.info('Parsed line object: ' + JSON.stringify(lineObj, '', ' '));
            csvArray[i] = lineObj;
            i++;
        } catch (e) {
            gs.error("in catch: " + e);
        }
    });

    gs.info('csvArray: ' + JSON.stringify(csvArray, '', ' '));
    outputs.users = csvArray;

    function cleanHeaders(h) {
        var ret = [];
        for (var i = 0; i < h.length; i++) {
            ret.push(h[i].replace(/ /g, "_"));
        }
        return ret;
    }
})(inputs, outputs);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

@Colin Wilson 

I tried your script in background script and it worked fine for both the files

It's something to do how your flow step and inputs are configured

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader