ctx.getVar()
Asynchronously reads variable values from the current runtime context. Variable resolution is consistent with {{ctx.xxx}} in SQL and templates, typically originating from the current user, current record, view parameters, popup context, etc.
Use Cases
Type Definition
Return Value: Promise<any>. Use await to get the resolved value; returns undefined if the variable does not exist.
If a path that does not start with
ctx.is passed, an error will be thrown:ctx.getVar(path) expects an expression starting with "ctx.", got: "...".
Common Variable Paths
ctx.getVarInfos()
Gets the structural information (type, title, sub-properties, etc.) of resolvable variables in the current context, making it easier to explore available paths. The return value is a static description based on meta and does not include actual runtime values.
Type Definition
In the return value, each key is a variable path, and the value is the structural information for that path (including type, title, properties, etc.).
Parameters
Example
Difference from ctx.getValue
In a JSField, use getValue/setValue to read/write the current field; use getVar to access other context variables (such as record, user, formValues).
Notes
- Path must start with
ctx.: e.g.,ctx.record.id, otherwise an error will be thrown. - Asynchronous method: You must use
awaitto get the result, e.g.,const id = await ctx.getVar('ctx.record.id'). - Variable does not exist: Returns
undefined. You can use??after the result to set a default value:(await ctx.getVar('ctx.user.nickname')) ?? 'Guest'. - Form values:
ctx.formValuesmust be retrieved viaawait ctx.getVar('ctx.formValues'); it is not directly exposed asctx.formValues. In a form context, prefer usingctx.form.getFieldsValue()to read the latest values in real-time.
Examples
Get Current Record ID
Get Record Within a Popup
Read Sub-items of an Array Field
Set Default Value
Read Form Field Values
Read URL Query Parameters
Explore Available Variables
Related
- ctx.getValue() - Synchronously gets the current field value (JSField/JSItem only)
- ctx.form - Form instance;
ctx.form.getFieldsValue()can read form values in real-time - ctx.model - The model where the current execution context resides
- ctx.blockModel - The parent block where the current JS is located
- ctx.resource - The resource instance in the current context
{{ctx.xxx}}in SQL / Templates - Uses the same resolution rules asctx.getVar('ctx.xxx')

