SAP Knowledge Base Article - Public

3369915 - How to parse CSV data from file

Symptom

I want to update a CSV file in a user task or in an initial form of a workflow and use its values on other tasks. How can I archive it?

Resolution

A JavaScript task can be used for parsing and preparing CSV data

If you already have the respective file object field in one of your forms, you need to add the respective file variable in the JavaScript task configuration via the option 'Add existing variable'. Then you can start the script. At first, load the libraries for handling files and parsing CSV data

const csv = require('csv')
const files = require('files')

Then you need to read your file (change the YOUR_CSV_FILE by the file variable name) and parse it using csv.parse with its input, options and desired data handling: csv.parse(input, options, data handling), e.g.

const options = { auto_parse: true, columns: true, relax_column_count: true }
const input = files.getContent(YOUR_CSV_FILE.id).buffer.toString('utf-8')

csv.parse(input, options, (error, myParsedCSV) => {
  if (error) {
    console.log(`Error parsing CSV: ${error}`)
  } else {
    //Show the number of rows in the console log
    console.log(`${myParsedCSV.length} rows read from CSV`)
    //Show parsed data of the second row in the console log
    console.log(myParsedCSV[1])
    //Export parsed data of the second row into a text variable
    myTextObject = JSON.stringify(myParsedCSV[1])
  }
})

If you define a text Variable (e.g. 'myTextObject') at the JavaScript task configuration, you will be able to use its value in the other parts of your workflow. 

For further information on this topic, please consult our documentation at https://help.sap.com/docs/signavio-process-governance/developer-guide/parsing-csv-data?locale=en-US

Please note that SAP Signavio Support cannot assist in developing scripts since this is not a part of the standard support. 

Keywords

JavaScript, CSV, file, parse, workflow , KBA , BPI-SIG-PG-JS , JavaScript for SAP Signavio Process Governance , BPI-SIG-PG-EDT , Process Governance Editor , How To

Product

SAP Signavio Process Manager all versions