In Workflow, how to access a value from a 'Data Mapping' step in a 'Run Script' step

Hello all,

Not sure if this is possible, but I’m trying to access a value from a ‘Data Mapping’ step in a ‘Run Script’ step.

In a nutshell, the Data Mapping step extracts data from a CSV file. I’d like to use the value from one of the columns in the CSV to determine which template should be used, using a ‘Run Script’ condition step. So say for expample, that column is called ‘TemplateToUse’ and the value is either ‘Template1’ or ‘Template2’.

Is this possible to do in Workflow?

Of course,

In your Execute Data Mapping step, set the Output Type to either Metadata, XML or JSON and the values will show in what comes out of it. Note that for Metadata, only values from first level fields will show, not details table.

Hi hamelj,

Thanks, I’ve set the Output Type now. How do I then access that output in the Script step?

If you output to XML or JSON, it will be the content of the data file now in memory so if you are trying to use in a Run Script action, you can select that content by opening the data file, for example:

var fso = new ActiveXObject("Scripting.FileSystemObject");
var inputFile = fso.OpenTextFile(Watch.GetJobFilename, 1);

OR

If you just want to load the entire content as a string, you could do:

var fileContent = Watch.ExpandString("%c");

Then do whatever you need depending upon the format of data you have. JSON is easiest to parse in JavaScript.

I’d say first look at the content of the output. While in debug mode, go step by step until after the Execute Data Mapping and then, from the Debug menu, choose View as Text..

Which one did you select? Metadata, XML, JSON? I suggest JSON as it is easy to handle from the script.

Hello,

Thank you both for getting back to me.

Took me a while to come back with an answer because I first had to learn to enter credentials on the ‘OL Connect Proxy Address’ page to get the Data Mapping stage to work! Hey ho… I’m very new to this system :smiley:

I’ve gone with JSON as I’m comfortable working with JavaScript. I’m running debug a step at a time and I can see that the output from the Data Mapping step is a json file with this structure:

[
{ schema: {xx},
id: xx
datasetid: xx
fields: {xx}
},
{ schema: {xx},
id: xx
datasetid: xx
fields: {xx}
},

]

the ‘fields’ object in each item of the array appears to be contain the data I’m after, specifically:

$_.fields.TemplateToUse

So if I’m understanding correcltly, would be script in the next step be something like:

var fso = new ActiveXObject("Scripting.FileSystemObject");
var inputFile = fso.OpenTextFile(Watch.GetJobFilename, 1);
if(inputFile.fields.TemplateToUse === "Template1"){
  Script.ReturnValue = 1;
} else {
  Script.ReturnValue = 0;
}

Actually more like this:

var inputFile = JSON.parse(Watch.ExpandString("%c"));
if(inputFile.fields.TemplateToUse === "Template1"){
   Script.ReturnValue = 1;
} else {
  Script.ReturnValue = 0; 
}

You need to make it a JSON object.

Hi,

So with that code, the variable ‘inputFile’ contains all the outputted JSON data (i.e. an array of objects, with each of those objects presenting an individual record). As written, the script fails as “inputFile.fields.TemplateToUse” on line 2 is null. I can get it to work by changing it to ‘inputFile[0].fields.TemplateToUse’, however, I don’t think that would be useful for my scenario as I want to check every record.

Would you say that I’m using the right approach here? I accept it’s entirely possible that I’m just misunderstanding how the Workflow works.

I’ve attached a screenshot of my Workflow in its current state. What I’m trying to achieve is the following:

  1. A csv file is dropped into the input folder.
  2. The ‘Data Mapping’ step runs and extracts all the data.
  3. For each record, the ‘Run Script’ step checks then checks the value of the ‘TemplateToUse’ and decides which template to use for each one.

I’m beginning to think that the ‘Run Script’ step needs to be inside a ‘for’ loop…

workflow

You can’t have multiple Templates be used for each record of a Datamapper result.

Your best bet would be to split your incoming CSV file for each change of Template before the Execute Data Mapping.

Then, after the Execute Data Mapping step, your script would set the name of the Template to use in a variable and in the Create Print Content, you use that variable as the name of your Template (don’t forget to add .OL-template as the file extension either in your script wheree you set the variable or directly in the Create Print Content plugin).

Make sure you set the Data Source and the JSON String value as shown in the image below.