Extract data with a regex

In this example, you will create an Xtension A set of instructions for Nintex Automation Cloud to use third-party API functionality with Nintex workflows. An Xtension may include workflow actions, start events, forms or file control. that asks a question of WolframAlpha, and use it to calculate the opportunity cost of business development proposals.

A proposal's opportunity cost is the value that would have been received had the proposal's expenditure not been spent: for example, by keeping the money in a high interest account for the same amount of time. If a proposal's expected return can't outperform its opportunity cost, it may be unsuited to the business's goals.

Calculating the future value of an investment accurately is a complex equation. In this example, you will:

  1. Use WolframAlpha to calculate the result for you.
  2. Extract the value from the response using the QueryJSON action.
  3. Use an Apply regular expression action to convert the value into a number for comparison.

The complete OpenAPI Specification A standard, language-agnostic description of RESTful APIs that can be read by both humans and machines. Formerly known as Swagger. and icon for this example are available here.

The WolframAlpha API

WolframAlpha interprets the language of a question and calculates answers based on its own store of data and algorithms. You can ask questions based on facts, figures and calculations, such as the distance between two cities, how many planes are currently above you, or how much a thousand dollars will be worth in a hundred years.

For more examples of questions you can ask, see https://www.wolframalpha.com/examples/

Create the Xtension

Step 1: Register for the WolframAlpha API key

  1. Sign in or create a new a developer account with WolframAlpha at developer.wolframalpha.com/portal/signin/html.
  2. Click on the My Apps tab.
  3. Click Get an AppID.
  4. Sign up for an AppID.
  5. Type in a name and description for your app.
  6. Record the App id when it is displayed: you will need it when creating a connection for your Xtension in Nintex Automation Cloud.

Step 2: Build the OpenAPI Specification

Build an OpenAPI Specification that:

For information on defining enums or multiple parameters, see Define the parameters

For more information on using x-ntx-summary, see Rename fields (x-ntx-summary).

 
{
    "swagger": "2.0",
    "info": {
        "version": "1.0",
        "title": "WolframAlpha",
        "description": "This example sends a question to WolframAlpha"
    },
    "host": "api.wolframalpha.com",
    "basePath": "/v2",
    "schemes": [ "https" ],
    "consumes": [ "application/json" ],
    "produces": [ "application/json" ],
    "paths": {
        "/query": {
            "get": {
                "summary": "WolframAlpha query",
                "description": "Answer a question",
                "operationId": "WolframAlpha",
                "produces": [ "application/json" ],
                "security": [
                    {
                        "myApiKey": []
                    }
                ],
                "parameters": [
                    {
                        "name": "input",
                        "in": "query",
                        "type": "string",
                        "required": true,
                        "description": "Type your question"
                    },
                    {
                        "name": "output",
                        "in": "query",
                        "type": "string",
                        "required": true,
                        "x-ntx-summary": "Select JSON format",
                        "enum": [ "json" ]
                    },
                    {
                        "name": "includepodid",
                        "in": "query",
                        "type": "string",
                        "required": true,
                        "x-ntx-summary": "Select Result",
                        "enum": [ "Result" ]
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "type": "object"
                        }
                    }
                }
            }
        }
    },
    "securityDefinitions": {
        "myApiKey": {
            "type": "apiKey",
            "name": "appid",
            "in": "query"
        }
    }
}

Construct the query

Step 1: Examine the JSON response

You can preview the response The return from a third-party API after a request has been made by the client. by copying the following into your browser and adding your API key:

http://api.wolframalpha.com/v2/query?input=continuous+interest+ PV%3D%243800%2C+rate%3D6%25%2C+periods%3D5&output= json&includepodid=Result&appid=YOUR_APP_KEY_HERE

The API returns a complex JSON object.

Step 2: Test your query

Create a JSON query to navigate to the plaintext key in the first subpods element:

queryresult.pods[0].subpods[0].plaintext

You can test your JSON JavaScript Object Notation: a data format based on JavaScript that is commonly used for API request parameters and responses. query using the Nintex Automation Cloud tools.

 
{
    "queryresult": {
        "success": true,
        "error": false,
        "numpods": 1,
        "datatypes": "Formula",
        "timedout": "",
        "timedoutpods": "",
        "timing": 1.964,
        "parsetiming": 1.2650000000000001,
        "parsetimedout": false,
        "recalculate": "",
        "id": "MSPa5961h4he2i1hh195db700004gg3d863ig3dchg5",
        "host": "http://www3.wolframalpha.com",
        "server": "7",
        "related": "http://www3.wolframalpha.com/api/v2/relatedQueries.jsp?
                    id=MSPa5971h4he2i1hh195db700006027ad7hdbc1ai61&s=7",
        "version": "2.6",
        "pods": [
            {
                "title": "Result",
                "scanner": "Formula",
                "id": "Result",
                "position": 100,
                "error": false,
                "numsubpods": 1,
                "primary": true,
                "subpods": [
                    {
                        "title": "",
                        "img": {
                            "src": "http://www3.wolframalpha.com/Calculate/MSP/
                                    MSP5981h4he2i1hh195db700003fh1732cgici61d8?
                                    MSPStoreType=image/gif&s=7",
                            "alt": "future value | Au$5129.46  (Australian dollars)",
                            "title": "future value | Au$5129.46  (Australian dollars)",
                            "width": 333,
                            "height": 36
                        },
                        "plaintext": "future value | Au$5129.46  (Australian dollars)"
                    }
                ],
                "states": [
                    {
                        "name": "Step-by-step solution",
                        "input": "Result__Step-by-step solution"
                    }
                ]
            }
        ],
        "assumptions": {
            "type": "FormulaSelect",
            "template": "Assuming ${desc1}. Use ${desc2} instead",
            "count": 2,
            "values": [
                {
                    "name": "PresentValueFutureValueContinuous",
                    "desc": "present and future value (continuous compounding)",
                    "input": "FSelect_**PresentValueFutureValueContinuous--"
                },
                {
                    "name": "AnnuityFutureValueLumpsum",
                    "desc": "annuity future value",
                    "input": "FSelect_**AnnuityFutureValueLumpsum--"
                }
            ]
        }
    }
}

Construct the regular expression

The plaintext from WolframAlpha is a string that includes some description with the final numerical value. To extract the value from the string, create a regular expression to match against positive or negative numbers with up to two figures after the decimal point: -?(0|([1-9]\d*))(\.\d+)?

Tip:  You can test your regular expressions at https://regex101.com/

For more information on regular expressions, see http://www.regular-expressions.info/quickstart.html

Build the workflow

Step 1: Add your Xtension

Import the OpenAPI Specification you created into Nintex Automation Cloud:

  1. Open your Nintex Automation Cloud tenancy.
  2. Click Xtensions in the dashboard to open the Xtensions page.
  3. Click  in the Private connector list.
  4. Click Choose a file. Navigate to the OpenAPI Specification on your computer.
  5. Wait for Nintex Automation Cloud to validate the file.
  6. Click Next.
  1. Nintex Automation Cloud detects the API key security template.
  2. Click Next.
  1. Edit the Name of the Xtension, which becomes the name of the action group in the Workflow designer.
  2. Edit the Description of the Xtension. This appears in the Private connector list in the Xtensions page.
  3. Select or upload an icon for the Xtension. This is displayed for each action or event in the Workflow designer.
  4. Click Publish.

Step 2: Create the workflow

For more information on creating workflows, see the Workflow Designer.

  1. Click Create workflow in your Nintex Automation Cloud tenancy.
  2. Configure the Start event to be a Nintex form with six variables:
    • The name of the person submitting the proposal (text).
    • The email address of the person submitting the proposal (text).
    • The description of the proposal (text).
    • The cost of the proposal (decimal).
    • The expected return on investment (decimal).
    • The number of months until that return is reached (integer).

    For more information on designing forms in Nintex Automation Cloud, see Design a form.

  3. Drag a Set a variable value action after the Start event, and create a variable to store your interest rate.
    For more information, see Set a variable value.

    Tip:  Storing your interest rate in a variable makes it easier to update the rate later.

  4. Drag a WolframAlpha query action after the Set a variable value action.
  5. Create a connection to the WolframAlpha API using your API key.
  6. Configure the WolframAlpha query action:
    • Type continuous interest PV=$[COST], rate=[RATE]%, periods=[MONTHS], using your public web form cost and months variables and interest rate variable.
    • Select JSON in the Select JSON format field.
    • Select Result in the Select Result field.
    • Create a collection variable to store the answer from WolframAlpha.
  7. Drag a Query JSON action after the WolframAlpha query action, and configure it to:
    • Query the WolframAlpha results variable.
    • Use the JSON query you created earlier.
    • Store the results in a new text variable.
      For more information, see Query JSON.
  8. Drag an Apply a regular expression action after the Query JSON action and configure it to:
    • Use the Query JSON result variable.
    • Use the Extract operation.
    • Use the regular expression pattern you defined earlier.
    • Ignore case.
    • Store the first result in a new decimal variable.
      For more information, see Apply a regular expression.
  9. Drag a Calculate a value action after the Apply a regular expression action and configure it to:
    1. Multiply the decimal result value from the regular expression by 1.
    2. Store the result back in the same variable.
      For more information, see Calculate a value.
  10. Drag a Branch by condition action after the Calculate a value action.
    For more information, see Branch by condition.
  11. Configure the Branch by condition action to test whether the regular expression result variable is less than the expected return on investment variable.
    Configure the two branches:
    • In the No branch, drag a Send an email action and configure it to send an email informing the person who submitted the proposal that it has not been accepted.
    • In the Yes branch, drag a Send an email action and configure it to send an email to the business managers with the proposal details, and carbon copy the proposal submitter.
  12. Click Test to test the workflow.
  13. Save or publish the workflow.

Tip:  If you want to troubleshoot an Xtension, select Development as the Assigned Use when you publish the workflow. Development workflows display more detailed error messages in their instance details. Republish your workflow as Production when you're ready to use it.