Using Jinja2 Dictionaries to streamline workflows.

Sam Peirce
Sam Peirce Administrator admin

Workflows sometimes require the implementation of complex decision-making processes in order to provide the desired outcomes. This can often lead to confusing tangles of conditional thresholds and update blocks that can be tedious and confusing to create.

While these are sometimes necessary, in cases where you need a workflow that says "If A, return 1; if B return 2, if C return 3, etc" you can simplify it using a Jinja2 Dictionary.

What is a Jinja2 Dictionary?

In Jinja2 (and in Python, which Jinja2 is based on), dictionaries (dicts) are used to store data that comes in Key:Value pairs. A typical dictionary looks something like this:

{
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}

Dictionaries (or analogous data structures) are a near universal data format used to represent objects in various programming languages. In Oomnitza, for example, all objects are represented as Dictionaries through the API.

How do these help my workflows?

Often workflows will require returning one value based off of another corresponding value. For example, Item Codes to Item Names, Abbreviations to Full Names, or Title to Role. When using these values to assign fields, storing them as a Jinja dictionary may help.

Example Case: User Position to Oomnitza Role

Often, organizations using Oomnitza will want to assign roles based on a user's title or position. This can involve a large variety of decisions to get to the final assignment outcome, often requiring lots of conditional thresholds. With a dictionary, this can be done with a single update block.

Our Sample Dictionary will look like this, where the Key (left) is Position and the Value (right) is role:

{
    "IT Manager": "25",
    "IT Specialist": "33",
    "IT Analyst": "33"
}

In this case, we want to give IT Managers the "Super Administrator" role in Oomnitza, and other IT Employees the "Hardware Asset Manager Role". For the workflow example, we'll give everyone else the role of employee. We're using the numeric code for each role because this will be used to update the role through the API.

The Workflow:

The workflow is relatively simple, which is why this process may be convenient. We have a begin block, a single API Block and our end block. Note that, for Role, we need to use the API block, as we're limited to a drop down in the update block and we want to use Jinja2 templating for this process. For other

The Begin Block

In this case, the begin block just triggers off of creation of users, but workflow of this type can be run on a schedule, triggered off of edits, or any other conditions that require it.

The API Block

The API Block for this workflow makes an update to the user who triggered this workflow in order to set their role.

Information

We begin with a get to the user so we can access their information.

Response

Aside from authorization, the only other data that needs to be populated is the Response, which then maps back to the Role field in the people module.


The full content of the response field is as follows:

{% set mappings = dict({"IT Manager": "1","IT Specialist": "33","IT Analyst": "33" }) %} {{mappings[response.position]}}

While it's been condensed to one line in the software, this information is easier to read when expanded:

{% set mappings = dict({
        "IT Manager":"1",
        "IT Specialist": "33",
        "IT Analyst": "33" 
    }) 
%}
 
{{mappings[response.position]}}

The first "set" portion of this template is enclosed in{%  %}, which indicates the execution of functions within Jinja2. We set a mappings variable equal to our dictionary, which we enclose in the parentheses of the dict() function. This takes the entered information and turns it into a dictionary.

The second portion of the template is enclosed in {{ }}, which prompts Jinja2 to print a value. In this case, we use {{mappings[response.position]}}, which takes the mappings dictionary, and returns the value that corresponds to user's position field in Oomnitza.

Wrapping Up

This is a particularly technical solution that demonstrates the flexibility of Oomnitza's workflow engine. If you have questions please let me know! I'd also love to see examples of your messiest workflows. Maybe we can help clean them up.