Back to How-Tos

Simulate multiple global variables in DataMapper

Original Author: Jean-Cédric Hamel

The Problem

In PlanetPress Suite 7, global variables are useful as they allow you to retain information and make them available through the whole job. Since the form contains both datamapping and template, and is processed as a whole, global variables are possible.

In Connect, due to the parallelization that allows multiple Weaver engine to run simultaneously for the same job, each Weaver has only access to the current record, making global variable more difficult to achieve.

When data from the first record is required to be available through the entire job, one could define variables in Workflow and then pass them on to Connect through the Pre-processor and properties. That is fine and it works.

Now for each Workflow variable, a Connect property will be required. When few variables are required, it is a viable solution. When many variables are required (50…100) then it becomes tedious and counter-productive.

The Solution

The actual solution, however, is to generate a JSON string in Workflow, assign it to a jobInfo (or a variable) and pass it on Connect through the pre-processor, then made available to the entire job, thus emulating the global variable.

How it's done

Primarily, a JSON string must be created in Workflow. Defining what’s JSON is outside the scope of this How-to, then again, for the sake of simplifying things, here the syntax expected from a simple JSON string:

{
 “var1”: ”value”,
 “var2”: ”value”,
 [...]
}

var1 and var1 are called the keys, whereas the part on the right is called the values.

The Workflow Process

enter image description here

Let's get into details of some of these tasks:

The Branch

Branching here allows executing a series of steps before going to the Connect plugins. It is important to change some settings of the branch. Double-clicking the branch brings up the following:

enter image description here

Uncheck Backup job information since we are using the jobInfo to store the JSON string.

Create File

enter image description here

Generate the JSON string here. Although it could be done all on a single line, it is suggested that each pair of key/value be put on different line for ease of reading. Data extraction can be done in the Create File plugin as explained here.

Set Job Infos and Variables

enter image description here

Select the %9 as the Var/Info#. Set the value to use %c which is the content of the actual file.

Delete

As usual, to discard a file we don't need (the JSON in this case) we can use the Delete task.

Connect Plugins

The original data file is sent to the Connect plugins as well as the jobInfo9.

The Data Mapping Configuration

In DataMapper, set a dummy JSON string as the Default Value for the JobInfo9 property. It is not mandatory but will be useful for the next steps.

enter image description here

Add an Extraction step. By default, it is a Javascript field, which is what is needed. Name the field and add the same Javascript code as in the image below. That set the field to the content of the JobInfo9.

enter image description here

At this point, the content of JobInfo9 that was defined as the default in a previous step should show as the value of the newly created field.

enter image description here

The Designer Template

Now in the Template, add a new script. enter image description here

Since the variable was defined without any keyword, it is now globally available to all scripts below it. Without getting into details, the JSON string is converted into a JSON object. It will allow direct access to the various key it contains has if they were actual field.

Add a placeholder on the Template. Add a Text Script with the following code. This is the code generated by Connect when we drag and drop a field from the Data Model.

enter image description here

Of course, the line field = global_JSON.customerNumber has replaced the one usually generated by Connect. When typing the variable global_JSON, notice that the period triggers the auto-complete of Connect, which brings back the list of all keys defined in the JSON string from Workflow.