addToContext - Cognigy Documentation

Syntax

addToContext(key, value, mode)

Description

Adds a value to the Context object. Parameters:

Returns: void

Example

Simple Mode

// Initial context: {}
addToContext("user.city", "Berlin", "simple");
// Context after execution: { user: { city: "Berlin" } }
// If called again with same key:
addToContext("user.city", "Munich", "simple");
// Context after execution: { user: { city: "Munich" } }  // Overwrites previous value

Array Mode

// Initial context: {}
addToContext("user.purchases", "book", "array");
// Context after execution: { user: { purchases: ["book"] } }

// Adding more items to the same array:
addToContext("user.purchases", "laptop", "array");
addToContext("user.purchases", "coffee", "array");
// Context after execution: { user: { purchases: ["book", "laptop", "coffee"] } }