Most application extensions need to use data from an Interaction. For instance, you may want to create an extension that shows a map of the user's current location. This type of information is stored in the Interaction’s variables. Another example of using Interaction variables is storing a security token received from the website containing the Interaction in an Interaction variable.

Jacada Interact provides the following APIs that allow application extension developers to work with Interaction variables:

  • getVariable: Gets the value of an Interaction variable so its value can be used in the application extension.
  • setVariable: Sets the value of an Interaction variable so the value is updated in the Interaction.

getVariable API 

This API gets the value of any user or system variable. Simple types, objects and collections (of simple types or objects) are supported. The variable is retrieved from the server when the API is called (asynchronous). 

The syntax of the getVariable  API is shown below. In the example, the value of the variable called textVar is written to the console. The handler for failure is optional, and may involve a failure handling of your choice.

ctx.getVariable('textVar', function(data) {
//success
console.log('tested var:', data);
}, function(xhr) {
//failure
});

setVariable API

This API sets the value of any user or system variable. Simple types, objects and collections (of simple types or objects) are supported. The value of the variable is updated when the end user clicks Next or Done. If the method fails, the Interaction continues without updating the value.

The syntax of the setVariable API is shown below. In this example, the new value of the variable is defined, and then the value of the variable called textVar is set to the new value.

var newValue = "aaaaaa"
ctx.setVariable('textVar', newValue);

A more complex use case involving updating the value of a simple type is to change the value of a user input (username) to a token before sending the variable to an authentication service. 

Using Variables in Extensions: Examples 

The following sections present simple examples that demonstrate how to manipulate objects (custom variable types) and collections. Each section provides one example for getVariable and one example for setVariable.

Working With Variables in a Simple Collection

The following example shows how to get the value of the first item in a simple collection (mySimpleCollection) and write that value to the console.

ctx.getVariable('mySimpleCollection', function(value) {
console.log(value[0]);
}, function() {
//handle error
}); 

The next example shows how to update the values of the variables in a simple collection. In this case, the new values are item1 and item2

ctx.setVariable('mySimpleCollection', ["item1", "item2"]);

Working With Object Fields

The examples in this section involve a custom variable type that contains a name field and an age field.

In the first example, we get the value of the name field, and use it to write a personalized greeting to the console. 

ctx.getVariable('Object Name', function(person) {
console.log('Hello, ' + person.name);
}, function() {
//handle error
});

The next example shows how to update the values of the object fields. In this case, the new values are Sami and 40

ctx.setVariable('Object Name', {name: 'Sami', age: 40})

Working With Object Collections

This section shows how to manipulate variables in a collection of objects. The following examples involve a collection (Employees Collection ) of two employees  objects. Each object contains a name field and an age field.

In the first example, we get the name of the first employee in the collection and write it to the console. 

ctx.getVariable('Employees Collection', function(employees) {
console.log(employees[0].name);
}, function() {
//handle error
})

In the next example, we define new values for the fields in the object collection, and then update the Employees Collection  with these new values.  

var employees = [{
name: 'Julia',
age: 30
},{
name: 'name',
age: 31
}];
ctx.setVariable('Employees Collection', employees);
 

 

Revision:

Comments