Skip to main content

Context Functions

The context object is automatically injected into every Transformer Task in Fenergo Integration Flows. It provides a rich set of built-in functions and helpers for logging, JSONPath querying, cryptography, and dynamic lookups.

This page documents all supported methods on context, grouped by category.

General

General functions for logging and raising errors.

FunctionDescription
context.log(message: string)Logs a message to Step Log Details in Execution Details. Useful for printing debug statements
context.loggingCentre.log(message: string, severity: string, additionalDetails?: string, errorCode?: string)Logs a message to the Logging Centre.
context.error(errorCode: string, errorMessage: string)Halts execution and raises a runtime error.
context.newGuid()Returns a new UUID string.
Using Context methods
context.log("Transforming input record");
// "Transforming input record" included in task's Step Log Details in Execution Details

const guid = context.newGuid();
// guid ➜ 6b29fc40-ca47-1067-b31d-00dd010662da

let input = input.inputValue ?? 0;
if (input < 1) context.error("100", "Input must be greater than zero.");
// Transformer task will fail with exception when input is less than 1

JSONPath

Access structured data using Jsonpath Library via the context.jsonath object.

FunctionDescription
context.jsonPath.query(obj: unknown, path: string, count?: number)Find elements in obj matching path Expression. Returns an array of elements that satisfy the provided JSONPath expression, or an empty array if none were matched. Returns only first count elements if specified.
context.jsonPath.paths(obj: unknown, path: string, count?: number)Find paths to elements in obj matching pathExpression. Returns an array of element paths that satisfy the provided JSONPath expression. Each path is itself an array of keys representing the location within obj of the matching element. Returns only first count paths if specified.
context.jsonPath.nodes(obj: unknown, path: string, count?: number)Find elements and their corresponding paths in obj matching pathExpression. Returns an array of node objects where each node has a path containing an array of keys representing the location within obj, and a value pointing to the matched element. Returns only first count nodes if specified.
context.jsonPath.value(obj: unknown, path: string, newValue?: unknown)Returns the value of the first element matching pathExpression. If newValue is provided, sets the value of the first matching element and returns the new value.
context.jsonPath.parent(obj: unknown, path: string)Returns the parent of the first matching element.

Example using the query method to select matching values.

Using Context JSONPath methods
const data = {
store: {
book: [
{ category: 'reference', author: 'Nigel Rees', title: 'Sayings', price: 8.95 },
{ category: 'fiction', author: 'Evelyn Waugh', title: 'Sword', price: 12.99 },
{ category: 'fiction', author: 'Herman Melville', title: 'Moby Dick', isbn: '0-553', price: 8.99 },
{ category: 'fiction', author: 'J.R.R. Tolkien', title: 'The Lord of the Rings', isbn: '0-395', price: 22.99 }
],
bicycle: { color: 'red', price: 19.95 }
}
};

const result = context.jsonpath.query(data, '$.store.book[?(@.price < 10)]');
/* result ➜
[
{ category: 'reference', author: 'Nigel Rees', title: 'Sayings', price: 8.95 },
{ category: 'fiction', author: 'Herman Melville', title: 'Moby Dick', isbn: '0-553', price: 8.99 }
]
*/

Cryptography

Cryptography methods for encrypting and decrypting strings.

FunctionDescription
context.crypto.aesEncrypt(plain: string, base64Authenticationkey: string, base64EncryptionKey: string)Encrypts plaintext string
context.crypto.aesDecrypt(cipher: string, IV: string, mac: string, base64AuthenticationKey: string, base64EncryptionKey: string)Decrypts encrypted string and returns plaintext value
context.crypto.calcHmac(message: string, authenticationKey: string, algorithm: string = "sha256")Calculates HMAC signature for given message using authenticationKey and algorithm.
Using Context Crypto methods
function transform(input: InputObject): ResultObject {
let response = {
response: {},
id: request.Id,
tenant: request.Tenant,
providerId: request.ProviderId,
type: requestType,
status: "Success",
errorDetails: null,
};

const encryptionResult = context.crypto.aesEncrypt(
JSON.stringify(response),
input.base64AuthenticationKey,
input.base64EncryptionKey
);
response = encryptionResult.cipher as any;
iv = encryptionResult.IV;
mac = encryptionResult.mac;

return {
output: {
response = encryptionResult.cipher as any,
iv = encryptionResult.IV,
mac = encryptionResult.mac,
},
state: {},
};
}

Lookups

Access Fenergo Reference Lists via context.lookups interface to automate reference data translation between systems

FunctionDescription
context.lookups.allReturns all Reference Lists as an array
context.lookups.getById(id: string)Retrieve specific Reference List by Reference ID (UUID as string)
context.lookups.getByName( name: string )Retrieve specific Reference List by Reference Name
context.lookups.getByItemById( lookup, id )Return return record in Reference List for given lookup Id
context.lookups.getItemByValue( lookup, value )Return return record in Reference List for given Lookup Value
context.lookups.getItemByCustomValue( lookup, columnName, value )Return return record in Reference List for given custom column and value
warning

Lookup functions are only available when lookups are enabled in Transformer Task Configuration.

See the example below how to use the Context to return the Country Reference List and then map the value from the Entity's Address to the ISO2 Country Code stored in the lookup id field.

Using Context Lookup methods
//Address mapping will use Country Reference List to convert Country Name to ISO2 Code
var lookupCountry = context.lookups.getByName("Country");

//Read first address from Entity record
var address = input.Entities[0].properties.addresses?.[0];

//Map Address.county to ISO2 value (stored in Reference List ID field)
var countryISO2 = context.lookups.getItemByValue(
lookupCountry,
address.country
);
context.log(
`Transform Address.Country: ${address.country} ISO2Code: ${countryISO2.id}`
);