Appearance
Script loading (ctx.script.load)
ctx.script.load(name) runs another Script of your tenant as a module and returns the value that Script returns. It replaces require, which does not exist.
text
ctx.script.load(name: string) -> module- It is synchronous and returns one value: the loaded module. There is no
okflag. - A Script that does not exist raises an error. Wrap the call in
pcallif the module is optional. - Each module runs once per run; loading the same name again returns the same value.
- A run can load at most 16 modules.
Writing a module
A module is an ordinary Script (role SCRIPT) that returns a table of functions:
lua
-- Script named "pricing-helpers"
local M = {}
function M.volume_discount(quantity)
if quantity >= 500 then return 0.15 end
if quantity >= 100 then return 0.10 end
return 0.05
end
return MKeep modules free of side effects: they run on load, and only once per run.
Using a module
lua
local QUANTITY = 120
local UNIT_PRICE = 18.5
local loaded, discount = pcall(function()
local helpers = coalesce(ctx.script.load("pricing-helpers"))
return helpers.volume_discount(QUANTITY)
end)
if not loaded then
ctx.log.info("pricing-helpers not installed, using the default rate", {
reason = tostring(discount),
})
discount = 0.05
end
return {
usedHelper = loaded,
netPrice = math.round(QUANTITY * UNIT_PRICE * (1 - discount), 2),
}The loaded module is not type-checked together with your script, so its exports arrive untyped. coalesce(value) passes the value through unchanged.
Availability
- In integrations,
ctx.script.loadresolves the libraries declared in the integration manifest, the integration's own scripts and the Script resources bound on the instance, not arbitrary tenant Scripts. - Product-template resolver scripts can load tenant Scripts.
- The print modules
lib/jdfandlib/jmfare also loadable in theSCRIPT,CUSTOM_APPandINTEGRATIONroles; see JDF and JMF.