Subsections of EXAMPLES

Automation

Complex 2-stage automation

This example demonstrates how to create a 2-stage automation process.

  • The first stage collects initial data to be able to generate new set of requests that cannot be performed using regular chaining functionality. Also, it demonstrates how to pass data to the next stage.
  • Second stage uses data that was passed from the first stage in combination with parent requests that was performed on the first stage.

First stage

{{- $warehouses := .Data.AccountAdminApi.Api.Requests.getWarehouses.Response.Get "data.warehouses" -}}
{{- $wIds := mkStringArray }}
{
    "postOperations": [
    {{- range $i, $warehouse := $warehouses.Array -}}
    {{- $wId := ($warehouse.Get "id").String -}}
    {{if $i}},{{end}}{
        "ErplyApi.Api.Post.getProductStock{{$i}}": "getProductStock",
        "ErplyApi.Api.PostParam.getProductStock{{$i}}.warehouseID": "{{ $wId }}",
        "ErplyApi.Api.PostParam.getProductStock{{$i}}.getSuggestedPurchasePrice": "1"
    }
    {{- $wIds = addToStringArray $wIds $wId -}}
    {{ end }}
    ],
    "stageTo": {
        "name": "pg-aut2-automation",
        "data": {"wids": {{ $wIds | toJson }}}
    },
    "enabled": true
}

Second stage

{{- $wids := (.Data.Automation.Request.Get "wids").Array -}}
{{- if .Data.Automation.ParentRequests.ErplyApi -}}
    {
    "postOperations": [
    {{- range $i, $wid := $wids -}}
        {{- $whStockResponse := index $.Data.Automation.ParentRequests.ErplyApi (printf "getProductStock%d" $i) -}}
        {{- $whStockWithNegativeAmounts := ($whStockResponse.Response.Get "records.#(amountInStock<0)#").Array -}}
        {{- if not $whStockWithNegativeAmounts -}}
            {{- continue -}}
        {{- end -}}
        {{if $i}},{{end}}{
            "ErplyApi.Api.Post.newRegistraton{{$i}}": "saveInventoryRegistration",
            "ErplyApi.Api.PostParam.newRegistraton{{$i}}.warehouseID": "{{ $wid }}",
            {{- range $ii, $el := $whStockWithNegativeAmounts -}}
            {{- $rowIndex := toInt (add $ii 1) -}}
            {{- $rowAmount := ($el.Get "amountInStock").String -}}{{if $ii}},{{end}}
                "ErplyApi.Api.PostParam.newRegistraton{{$i}}.productID{{$rowIndex}}": "{{$el.Get "productID"}}",
                "ErplyApi.Api.PostParam.newRegistraton{{$i}}.amount{{$rowIndex}}": "{{ replace $rowAmount "-" "" 1 }}",
                "ErplyApi.Api.PostParam.newRegistraton{{$i}}.price{{$rowIndex}}": "{{$el.Get "suggestedPurchasePrice"}}"
            {{- end -}}
        }
    {{- end -}}
    ],
    "enabled": true
}
{{- end -}}

JSON query (gjson)

There is no way to match multiple values in one run, but you can use the pipe | operator to run multiple searches in a row.

Query sample

friends.#(first!=Dale)#|#(first!=Roger)#.last
// Result: ["Murphy","Smith"]

Code sample

In template code:

{{ $res.Get "friends.#(first!=Dale)#|#(first!=Roger)#.last" }}
// With more pipes:
{{ $res.Get "friends.#(first!=Dale)#|#(first!=Roger)#.last|@unique|@commaSepStr" }}
// Result: "Murphy,Smith"

While chaining requests:

<input 
    type="hidden"
    name="CustomApi.Api.Query.req2.<-names"
    data-preset-val="req1.Response.friends.#(first!=Dale)#|#(first!=Roger)#.last|@unique|@commaSepStr">

JSON payload for references

{
  "name": {
    "first": "Tom",
    "last": "Anderson"
  },
  "age": 37,
  "children": [
    "Sara",
    "Alex",
    "Jack"
  ],
  "fav.movie": "Deer Hunter",
  "friends": [
    {
      "first": "Dale",
      "last": "Murphy",
      "age": 44,
      "nets": [
        "ig",
        "fb",
        "tw"
      ]
    },
    {
      "first": "Roger",
      "last": "Craig",
      "age": 68,
      "nets": [
        "fb",
        "tw"
      ]
    },
    {
      "first": "Jane",
      "last": "Murphy",
      "age": 47,
      "nets": [
        "fb",
        "ig"
      ]
    },
    {
      "first": "John",
      "last": "Smith",
      "age": 49,
      "nets": [
        "tw"
      ]
    }
  ]
}

Public/b2b emailing

Problem with sending emails from public/b2b pages

The regular way exposes all fields to the template - this means anyone can alter the field contents before its being sent. This allows the area to be used for possible phishing attacks.

Secure fields with automations

The best way to hide the fields is to move all functional fields away from the template (in this case into an automation) These automation fields are never exposed to the public and cannot be intercepted.

The following example contains 3 templates:

  1. check-page - the page where the user inputs the email (public page)
  2. check-emailer-automation - the automation tha actually makes the email send
  3. optional - just for an example we parse a template into the email content in the automation (not public)

check-page

In this example we only render the form when nothing is sent, we also never expose the automation trigger to the template, thus a user cannot even alter its execution.

We use the input here to trigger the automation, and the only value that is being passed to the e-mailer.

<h1>Secure emailer</h1>
<p>Email from public/b2b without exposing the fields for editing</p>

<!-- Optional, we render the form only once and display a message once its already sent -->
{{ if .Data.Parameters.inputEmailField }}
    {{ .Tools.AutomationEvent "check-emailer-automation" (jsonSet `{}` "email" .Data.Parameters.inputEmailField) }}

    <h2>Thanks!</h2>
{{ else }}
    <h2>Send me something</h2>
    <form method="post">
        <label for="email">Add your email</label>
        <input type="string" id="email" name="inputEmailField">

        <button type="submit">Send</button>
    </form>
{{ end }}

{{ .Data.Parameters.inputEmailField }}

check-emailer-automation

We read the email from the passed data and use it as the email, rest is hardcoded into the automation. Here we also encode an optional template to the email body.

{
    "postOperations": [
        {
            "EMSApi.SendEmailInput.To": "{{ .Data.Automation.Request.Get "email" }}",
            "EMSApi.SendEmailInput.Subject": "Secure emailer demo",
            "EMSApi.SendEmailInput.ContentType": "html",
            "EMSApi.SendEmailInput.Content": "<encode>{{ .Tools.ExecTemplate "check-done-page" }}</encode>",
            "EMSApi.SendEmailInput.IsEncoded": "true"
        }
    ],
    "enabled": true
}

(optional) check-done-page

The content that will be in the email.

<h1>Thanks! email received</h1>