eval
The eval
function is used evaluate Javascript scripts
Keep in mind that Javascript scripts are not STEP Business Rules. There is no access to STEP API function such as Managers, Nodes, ...
Syntax
c4i:eval(scriptId, [params]])
Parameters
scriptId
- STEP execution : The ID of the asset containing the JS script
- Standalone execution : The path to the JS script
params (optional)
Script parameters. The parameters are available through bindings named $1
, $2
, ...
See examples below
Return value
The result of the script execution. Javascript objects will be converted to JSON objects
Context
A binding named context
is available during script execution.
The following methods are available on the context
binding :
store(key, value)
: Store a key-value pair the same way as the store doesstoreJson(key, value)
: Store a JS object as JSONgetStoredValue(key)
: Gets a stored value from a keygetCurrentContextId()
: Returns the current context ID. See currentContextIdgetExportTime()
: Returns the current export time. See exportTime
Control execution order using the order custom function.
Parameters
Any XPath, executed against the currently mapped node, can be used as a parameter for script evaluation.
Some important remarks :
- XPath functions such as string provide String objects
- Otherwise, general XPath evaluation provide instances of NodeList
Return values
Unlike STEP Business Conditions, JS scripts do not need to explicitly return a value. The script evaluation is its return value.
For example, if you want to return a String value, the script should be :
"Example return value";
And not :
return "Example return value";
Example
Simple example without parameters:
The following example illustrates how to return a String value :
- Product[@UserTypeID="PROD_ARTICLE"]:
product[id]:
- ./@ID : id
- c4i:eval("simpleScript"): scriptResult
"Example return value"
String parameter example
The following example illustrates how to manipulate a provided String parameter
- Product[@UserTypeID="PROD_ARTICLE"]:
product[id]:
- ./@ID : id
- c4i:eval("scriptParameter", string(Name/text())): scriptResult
/**
*
* @param {string} name
* @returns {*}
*/
function scriptParameters(name)
{
return name.replace("Test", "").trim();
}
scriptParameters($1);
Manipulate a NodeList returned by XPath evaluation and return JSON data
The following example illustrates how to manipulate a NodeList returned by XPath evaluation and return JSON data
- Product[@UserTypeID="PROD_ARTICLE"]:
product[id]:
- ./@ID : id
- c4i:eval("scriptListParameters", Values): scriptResult
/**
*
* @param {org.w3c.dom.Element} values
* @returns {*}
*/
function scriptListParameters(values)
{
var result = {};
var valuesList = values.getChildNodes();
for (i = 0; i < valuesList.getLength(); i++)
{
var value = valuesList.item(i);
var attributeId = value.getAttribute("AttributeID");
if (String(attributeId) === "Length")
{
result[attributeId] = Number(value.getTextContent());
}
}
return result;
}
$1.getLength() === 0 ? null : scriptListParameters($1.item(0));
Store JSON data
The following example illustrates how to return JSON data and how to reuse it later using the json custom function.
In this example, the .family
mapping rule is evaluated but its output is ignored. This is why we can return null
in the JS script after storing the JSON value.
The order
function is used to make sure that the .family
mapping rule is executed before the product
mapping rule.
- Product[@UserTypeID="PROD_FAMILY"]:
.family[id]:
- ./@ID : id
- c4i:order(1, c4i:eval("scriptStoreJson", string(@ID), string(Name/text()))): scriptResult
- Product[@UserTypeID="PROD_ARTICLE"]:
product[id]:
- ./@ID: id
- c4i:json(c4i:getStoredValue(string(./@ParentID))): parent
function scriptStore(id, name)
{
context.storeJson(id, {id: id, name: name});
return null;
}
scriptStore($1, $2);