As part of a project with a client, I am developing a workflow on their engine (built partially on top of node.js) and I have a problem with the Javascript task. In particular, I am taking a csv-file accountCsv
as an input, then I want to modify a process variable outputFinanceAccounts
which should be a string containing some information read from the csv-file. Further, I want to pass this process variable to the next activity of the workflow.
Inside the Javascript task, I am using the module csv.parse
(npm package that is available on the client's engine), and in general it works well and reads the data from the csv file correctly. This function uses an async callback function as an argument. Further, I am changing the process variable outputFinanceAccounts
inside this async function. During testing I see that this process variable is assigned with correct value inside the parsing block. However, when the csv.parse
function is finished working, this updated value of the process variable outputFinanceAccounts
is lost. Therefore, I can’t pass any value of this process variable to the next activity of the workflow.
I tried to create a nested function inside csv.parse
and return the result of this function outside of csv.parse
, but it does not work. From what I understand, this trick would work only if csv.parse
would be a function, which it is not really, and I don't know what would be the right syntax for that.
const csv = require('csv');
const files = require('files')
const DepartmentColumn = 'Department'
const options = {auto_parse: true, columns: true, relax_column_count: true }
// Read the reportCsv file (workflow variable)
const csvContents = files.getContent(accountCsv.id)
outputFinanceAccounts = 'VALUE BEFORE PARSING'
console.log('Before parsing', outputFinanceAccounts);
csv.parse(csvContents.buffer.toString('utf-8'), options, (error, AccountList) => {
if (error) {
console.log(`Error parsing CSV: ${error}`)
return
}
if (AccountList.length == 0) {
console.log(`Error: CSV contains zero rows`)
return
} else {
console.log(`${AccountList.length} rows read from CSV`)
}
AccountList.forEach(account => {
if(account[DepartmentColumn] == 'Finance'){
outputFinanceAccounts = "VALUE INSIDE PARSING";
console.log('Inside parsing', outputFinanceAccounts);
}
});
})
console.log('After parsing:', outputFinanceAccounts);
Testing the code on the client's workflow engine (built partially using node.js) gives the following results:
Test execution finished successfully
Logs
-> Before parsing VALUE BEFORE PARSING
-> After parsing: VALUE BEFORE PARSING
-> 4 rows read from CSV
-> Inside parsing VALUE INSIDE PARSING
-> Inside parsing VALUE INSIDE PARSING
But what I need to have is:
-> After parsing: VALUE INSIDE PARSING
Thanks in advance for helping me with this.