APIs to manipulate context variables

The session object allows access and manipulation of context variables through operations by following the session.context.operation() syntax.

context
The context that a session object provides access to. The context can be input, output, or a named context that is retrieved by the session.name() API.
operation
An API that is used with context variable. The following APIs are supported.

The availability of context variables differs in the various built-in contexts. The input and output contexts do have context variables but the NULL and OUTPUT contexts do not have context variables. When you access context variables, a good practice is to specify a named context explicitly.

deleteVariable()

Deletes the variable from the context.

Syntax
session.context.deleteVariable(name);
Parameters
context
The name of a context where the value is obtained: input, output, or a named context.
name
The name of the variable to access.
Guidelines
Delete the name context variable from the context context. Returns true, if successful, and false, if an error occurs.
Examples
  • Delete the pname variable in the myCtx context.
    session.name('myCtx').deleteVariable('pname');
  • Delete the myVar context variable from the myCtx context.
    session.name(myCtx).deleteVariable(myVar);

getVariable()

Gets the value of a context variable.

Syntax
session.context.getVariable(name);
Parameters
context
The name of a context where the value is obtained. The name of the context can be session.input, session.output, or a named context that is retrieved by the session.name() API.
name
The name of the context variable to access.
Guidelines

Gets the value of a context variable. Returns undefined, if the context variable does not exist. The value that is returned can be a JavaScript primitive value or a JavaScript Object (such as a Buffer, Buffers, or NodeList). The NodeList can be manipulated by using the XML DOM APIs provided.

A context variable is assigned with a value and then associated with a processing context. The context variable can be written or read by different processing actions within the scope of a transaction. Processing includes such actions as set variable, transform, or GatewayScript. When done between different types of processing actions, data conversions take place before marshalling and unmarshalling context variables. For most data conversions, the data that is read is the same data that is written. A few cases result in data that is different from the data that is originally set into a context variable.
Table 1. Data conversion exceptions.
Data Conversion
JavaScript Datetime. Returns a JavaScript String for a context variable that was originally set to a JavaScript Datetime.
JavaScript Buffers. Returns a JavaScript Buffer for a context variable that was originally set to a JavaScript Buffers.
XML DOM Node. Returns a JavaScript NodeList for a context variable that was originally set to a JavaScript Node.
Some JavaScript Object methods and properties. If a JavaScript object that has methods and properties is set to a context variable, returns a new JavaScript object.
Examples
  • Assign the name1 context variable from the anothercontext context to the contextvar variable.
    var contextvar = session.name('anothercontext').getVariable('name1');
  • Read the myVar context variable from the myCtx context.
    session.name(myCtx).getVariable(myVar);

listVariables()

Returns an array of variable names.

Syntax
session.context.listVariables();
Parameters
context
The name of a context where the value is obtained. The name of the context can be session.input, session.output, or a named context that is retrieved by the session.name() API.
Example
Return the variable names in the mycontext context.
let contextvars = session.name('mycontext').listVariables();

setVariable()

Sets the value of a context variable.

Syntax
session.context.setVariable(name, value
Parameters
context
The name of a context where the value is obtained. The name of the context can be session.input, session.output, or a named context that is retrieved by the session.name() API.
name
The name of the context variable to access.
value
The value to set in the context variable. The value can be a JavaScript primitive value or a JavaScript Object (such as a Buffer, Buffers, XML DOM Node, or NodeList).
Guidelines

The setVariable() API sets the value of a named context variable with a new value. Returns true if successful. An error occurs if the value is undefined or null.

A context variable is assigned with a value and then associated with a processing context. The context variable can be written or read by different processing actions within the scope of a transaction. Processing includes such actions as a set variable, transform, or GatewayScript. When done between different types of processing actions, data conversions take place before marshalling and unmarshalling context variables. Most data conversions are not apparent; that is, the data that is read is the same data that is written. A few cases result in data that is different from the data that is originally set into a context variable.
Data conversion exceptions.
Data Conversion
JavaScript Datetime. Returns a JavaScript String for a context variable that was originally set to a JavaScript Datetime.
JavaScript Buffers. Returns a JavaScript Buffer for a context variable that was originally set to a JavaScript Buffers.
XML DOM Node. Returns a JavaScript NodeList for a context variable that was originally set to a JavaScript Node.
Some JavaScript object methods and properties. Returns a new JavaScript object.