Parsware
All articles

Business rules

Environment variables

The same solution in two environments should call two different payment services, a sandbox in one and the real thing in the other, without anybody editing a script. That's what an environment variable is for. We installed one solution in two environments, gave each its own value, and saved the same order in both.

The Environment variables list of the Calling out solution in the Test environment: Mail service and Payment service, each with its own address, next to the title "Environment variables"

A solution moves from environment to environment: built in Development, tried in Test, used in Production. It's meant to be the same solution everywhere. But some things it does must differ. In Test it should take payments through a sandbox; in Production, through the real provider.

If the address is in the script, you have two bad choices: edit the script after every import, or remember to. An environment variable takes the address out of the script. The solution says there is a payment service. Each environment says where.

We used the Calling out sample from calling an outside service. Import it from samples/solutions/business-rules-outbound-calls to follow along. It declares two variables, Payment service and Mail service, and takes a payment whenever an order is set to Paying.

Two halves

A variable is split in two, and the split is the whole idea:

  • The declaration: its name, type, description and optional default. It belongs to the solution, and travels with it.
  • The value: this environment's answer. It belongs to the environment, and never leaves it. Exporting the solution doesn't carry it; importing the solution doesn't overwrite it.

We had the sample installed in our Test environment already, with both values set. We imported the same package into a second environment, Stock. In Stock, the solution's Environment variables looked like this:

The Environment variables list in Stock: Mail service and Payment service, both Text, both Not set

Not set, both of them. The package brought the declarations and nothing else. Meanwhile, in Test, the same solution kept its own values:

The Environment variables list in Test: Mail service set to http://localhost:5990/mail and Payment service set to http://localhost:5990/pay

Not set is loud

In Stock, before setting anything, we saved an order for 49.90 with its status set to Paying:

A new order ORD-1042 refused with "The environment variable 'par_payment_service' has no value in this environment, so there is nowhere to call. An administrator sets it under Environment Variables."

Refused, with a sentence that names the variable and says who fixes it. That's deliberate. A missing setting is a mistake to fix before go-live, not something to paper over, and the Not set in the list is the checklist.

Each environment its own value

We selected Payment service in Stock and chose Set value:

The Set value panel for Payment service in Stock, explaining the value belongs to this environment alone, with the value http://localhost:5990/decline

For this article we pointed Stock at a provider address that declines every card, so the difference is easy to see. Then we saved the same order again in each environment.

In Stock:

The order ORD-1042 in Stock refused with "The payment service refused this order (402)."

In Test:

The order ORD-1042 in Test saved: Status Paid, Payment reference PAY-915629, Confirmation email Sent

Same solution, same script, same order. Stock asked its provider and was declined. Test asked its provider, was paid, stamped the reference and sent the confirmation. Nobody edited a script, and neither environment knows the other's addresses.

Reading one in a script

The sample's script never contains an address. It names the service:

var answer = callService('par_payment_service', body);

callService and sendEmail look the name up in the environment they run in. For any other setting, a script reads the value directly:

var limit = getEnvironmentVariable('par_approval_limit');
if (limit == '') {
  throwValidationError('The approval limit is not set in this environment.');
}

An unset variable reads as an empty string, so check for it and say so.

Where you can read one today: plugin steps, Custom APIs and scheduled jobs, which all run on the server. Form rules and processes can't read environment variables yet. For a form rule, that means a value the rule needs still has to be written into it.

The declaration, as the solution stores it

The sample's two declarations are short documents, and the description is where they explain themselves:

{
  "logicalName": "par_payment_service",
  "displayName": "Payment service",
  "publisherPrefix": "par",
  "description": "Where callService posts a payment. Declared by the solution; pointed at a sandbox in Dev and at the real provider in Prod, which is the whole reason the endpoint is not in the script.",
  "valueType": "Text"
}

There is no value in it, and no default, on purpose. The mail service's description says why in one line: an environment that has configured no mail service should fail loudly rather than quietly post nowhere.

What belongs in a variable

A variable is for something the solution needs and each environment answers differently. Good candidates:

Variable Type Development Production
Payment service address Text A sandbox The live provider
Mail service address Text A capture service The real mail provider
Approval limit Number 10, so testers hit it easily 1,000
Send customer emails Boolean No Yes
Branch codes to sync JSON ["TEST"] ["LDN", "MAN", "BRS"]

And what doesn't belong in one:

  • Business data people change, such as approval bands per department, or a price list. That is a table, edited by the people who own it, with history and security. Capital planning keeps its approval thresholds in one.
  • Something that is the same everywhere, such as the number of days in a week. Write it in the script.
  • Anything a form rule needs. Form rules can't read variables yet, so a value they depend on still lives in the rule, or in a field the server fills in.

A checklist for go-live

Because a solution carries declarations and never values, a new environment starts with every variable Not set. That makes the Environment variables list a go-live checklist:

  1. Import the solution into Production.
  2. Open Environment variables in the solution. Every row that says Not set is a decision still to make.
  3. Set each one, reading its description. This is why descriptions are worth writing well: the person setting Production's value is often not the person who wrote the script.
  4. Try the thing each variable is for, such as one real payment for a small amount, before telling anybody it is live.

Later imports of the same solution leave the values alone, so this is done once per environment, not once per release.

A variable in a script, end to end

A step that respects a per-environment switch, only email customers in Production, reads the switch and acts on it:

var send = getEnvironmentVariable('par_send_customer_emails');
if (send == 'true') {
  sendEmail('par_mail_service', {
    to: getValue('par_customer_email'),
    subject: `Order ${getValue('par_name')} is on its way`,
    body: 'Thank you for your order.'
  });
}

getEnvironmentVariable answers text, whatever the variable's type, so a Boolean is compared with 'true', and an unset one is ''. The comparison treats anything other than 'true' as don't send, which is the safe way round for a switch that emails customers. (The variable here isn't in the sample.)

Secrets

A variable can hold a value that must not be widely seen, such as a key a provider issues. Two habits apply:

  • Never give a secret a default. A default travels with the solution, so it would be in every export.
  • Keep secrets per environment. A sandbox key in Test and the live key in Production, each set by hand in its own environment, never copied through a file.

Values aren't exported, so a solution file shared with a colleague, or kept in source control, never carries them.

Questions that come up

Can two solutions share one variable? A variable belongs to the solution that declares it. A second solution that needs the same service can name it in its scripts, as long as both are installed; or declare its own, if the two should be set separately.

What happens to a value if I uninstall the solution? The declaration goes with the solution, and the value with it. A reinstall starts from Not set again.

Can I see which scripts use a variable? Search the solution's steps, APIs and scripts for its logical name. Keeping the logical name distinctive, such as par_payment_service, makes that search reliable.

Declaring your own

In your solution, Environment variables, New:

  • Display name and Logical name, prefixed with your publisher's prefix.
  • Type: Text, Number, Boolean or JSON. A value is checked against it where it's typed, so a wrong one is refused in the panel, not found later by a script.
  • Description: worth writing well. The person setting the value in Production often isn't you.
  • Default value, optional. It travels with the declaration. Leave it empty for anything with no sensible default, such as an address or a secret, so an unset one shows as Not set rather than quietly using the wrong thing.

Try it

Set Stock's Payment service to the same address as Test's and save the order again: it's paid. Then export the solution from Test, import it over Stock's copy, and check the value you set in Stock is still there.

Next: data policies: rules about which records a permission reaches, and which saves it refuses.