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 -}}