Create a Custom Variable in a Service

The Set Variables handler enables you to create custom variables as part of a MEP service. This handler will also update the variable's value whenever it is triggered in the service.

When creating a variable using this handler:

  1. Set the scope using the drop-down list.
  2. In the Key field, enter a key name that, along with the scope, is how you will reference the variable in an expression. This can include any character from A-Z and the dot, underscore and hyphen characters (“._-”). It is not case-sensitive.
  3. Set the value for the variable. In most use cases, the value will be an expression. This expression can be self-referential if a value is set for the Initial value on creation. Alternatively, the value set here can be an alphanumeric string.
  4. If the value is a self-referential expression, then you'll need to set the Initial value on creation.

Understanding the initial value on creation

This is the starting value for the variable. It is not incremented the first time the handler is triggered (but incremented every time after this). For example, if you are creating a variable that counts the number of times it is triggered then enter “1” in this field.

Note that the chosen scope very important in determining the interaction between the Initial value on creation and Value fields. For example, if the scope is userservice, then each separate end user has their own value for the variable stored for their use of a particular service. For a self-referential userservice variable, each new end user starts with the Initial value on creation value. However, if the scope is service then the service only has one value stored for the variable. Self-referential service variables increment the value each time the variable is triggered in the service.

Example of creating a custom variable

A service asks users to send in a friend's phone number (and then sends it back to them). The Set Variable handler stores the number, enabling the service to retrieve it in subsequent handlers, such as to send a message to the number, send the number back to the user, or forward the number onto an external server using the HTTP Poke handler.

The Set Variable handler creates a variable called session.friendsNumber. The handler has the following properties:

  • Scope: session

    This creates and holds the value for just the session.

  • Key: friendsNumber

  • Value: ${af:getPhoneNumber(request.defaultMessage)}

    This expression looks for a phone number in the message body sent by the end user