When To Use Event Scripts
Use an event subscription when the data definition needs behavior, not just structure:- Derive several sibling attributes after a reviewer edits one value
- Normalize data before formulas, validation, and selection rules run
- Call a Service Bridge to enrich a row from an external system
- Create or close data exceptions based on business logic
- Copy or move data objects when a modeled relationship changes
- Emit a named event that another subscription can handle
Where Event Scripts Run
Event subscriptions are attached to group data elements in a Data Definition. In configuration, those group elements are stored astaxons. At runtime, the script executes for one data object instance of that group.
The script runs inside the recalculation flow. Writes made by the script are visible to downstream formula recalculation, selection option evaluation, validation, selection validation, and conditional formatting.
Configuration
AttacheventSubscriptions to a group data element:
Attribute Change Shorthand
For simple attribute-change subscriptions,dependsOn is a compact way to list the attributes that should trigger the script:
on form when you need to match multiple event types or a custom event pattern.
Fields
string
required
Unique name for the subscription within the owning group data element. The runtime uses this name for loop-control state and diagnostics.
string
required
Regex pattern matched against the full event string. Kodexa anchors the pattern internally, so
changed:dataAttribute:amount matches only that event string.string[]
Convenience field for
changed:dataAttribute. When present with on: "changed:dataAttribute", validation converts it to an on regex and clears dependsOn.string
required
JavaScript source executed for the subscription run.
boolean
default:"false"
When
true, the subscription remains in the Data Definition but is skipped at runtime.Supported Event Strings
The subscription matcher uses full event strings.changed:dataAttribute fires when an attribute first receives a value, not only on later edits. Setting a value on a previously blank field is treated as a change, so a reviewer’s first pick into an empty field runs the matching subscription just as a subsequent edit would.Runtime Rules
Execution Order
When a data attribute changes, Kodexa processes the cascade in this order:tax_rate, formulas and validations that depend on tax_rate see the value written by the script.
Script Globals
Every event subscription receives a small set of prebound globals.event
The event object describes why the script ran.
Example:
currentObject
currentObject is the data object instance for the group data element where the event matched.
Reading Values
Writing Values
UsesetAttribute(name, value) for normal writes. It finds or creates the attribute, resolves the target type from the Data Definition when available, writes the typed value, persists the change, and notifies the recalculation system.
Payloads For Service Bridges
payload(mapping) builds a plain JavaScript object from attributes on currentObject.
Creating Child Data Objects
UsegetOrCreateChild(path, opts?) or addChild(opts) when the event should create modeled child data.
copyAttributesFrom(sourceObj, mappings, ownerUri?) when moving known attributes between objects:
DataAttribute
getAttributeByName() and getAttributesByName() return attribute wrappers for lower-level operations.
Prefer
currentObject.setAttribute(...) for common writes. Use DataAttribute methods only when you need direct access to a specific attribute instance.
document
The document global is the active KDDB document.
Data Object Access
Document Metadata And External Data
Content, Search, And Serialization
Exceptions And Validations
Example:
serviceBridge
serviceBridge.call(bridgeRef, endpointName, body?) calls an external system through a configured Service Bridge.
orgSlug/bridgeSlug. Some runtimes can supply a default organization slug, but fully qualifying the bridge keeps scripts portable.
The bridge response is parsed as JSON. If the platform proxy returns a result envelope, the runtime returns the result value directly. If the proxy returns an error envelope, the script fails with that error.
taxon
In the browser subscription runtime, taxon.optionLabel(taxonName, value) returns the display label for a selection option value.
bridge
The browser subscription runtime exposes bridge for UI notifications and explicit event emission.
Logging
Use structured logging during development and troubleshooting:console.log, console.warn, and console.error are also available.
Loop Control
The recalculation service protects event scripts from runaway cascades:
Design scripts to be idempotent. A script may run more than once over the life of a document, and retries should not duplicate data or re-open resolved work.
Examples
Normalize And Derive Sibling Values
Service Bridge Enrichment
Create A Data Exception
Emit A Follow-Up Event
Constraints
Best Practices
Start with the event string
Start with the event string
Name the exact business event the script responds to. Prefer specific
on patterns such as changed:dataAttribute:(quantity|unit_price) over broad matches.Keep the script local to the group
Keep the script local to the group
Read and write values on
currentObject when possible. Use document only when the script really needs broader document context.Make writes idempotent
Make writes idempotent
Write the same result for the same inputs. Avoid appending duplicate child objects or recreating exceptions without checking existing state.
Use typed values
Use typed values
getFirstAttributeValue returns typed values. Preserve those types when writing with setAttribute.Guard browser-only helpers
Guard browser-only helpers
Wrap
bridge and the taxon helper usage in typeof ... !== "undefined" checks when a script may also run outside the browser runtime.Log the decision, not every value
Log the decision, not every value
Use
log.debug while building and keep production logs focused on decisions, external calls, and unexpected states.Related
Data Definitions
Define the data model that event scripts operate on
Data Definition Structure
Configure data elements, groups, validations, selection options, and event subscriptions
Selection Option Formulas
Compute dynamic selection options from data and bridge calls
Service Bridges
Configure external API calls used by event scripts
