Scalar-function rule
Introduction
Rules in Casewhere are not limited to boolean predicates. A scalar-function rule performs logic and returns a value of any type. Extracting computing logic into reusable rules is a best practice — it keeps workflows clean and makes the logic available across different parts of your solution.
Rule syntax
Keywords
The following keywords are available in scalar-function rules:
| Name | Type | Description |
|---|---|---|
| @user | DslUser | The user in context. See access rule for the full DslUser reference. |
| @parameters | DynamicDataObject | The parameters passed to the rule. Access attributes using the dot operator (e.g., @parameters.address). |
| @apiFactory | IManagedApiFactory | To construct Casewhere API objects. Read here for the full list of available APIs. |
Examples
Example 1: Unit conversion
A rule named "Convert Meter To Centimeter" that converts a meter value to centimeters:
@parameters.meterValue * 100
Evaluate the rule in a workflow scripted activity using IRuleApi:
var ruleApi = ctx.Use<IRuleApi>();
var parameters = new Dictionary<string, object>();
parameters["meterValue"] = 1;
var cm = ruleApi.Eval<int>("Convert Meter To Centimeter", parameters);
Example 2: Reusable data query
A rule named "Find Products By SKU" that queries products by their SKU code:
#predicate
var dataApi = @apiFactory.Get<IDataApi>();
var filter = FilterBuilder.Create().Eq("SKU", @parameters.SKU).Build();
var result = dataApi.Search(DataObjectApiQuery
.For("Product")
.FilterBy(filter));
return result.Data;
Evaluate the rule in a workflow scripted activity using IRuleApi:
var ruleApi = ctx.Use<IRuleApi>();
var parameters = new Dictionary<string, object>();
parameters["SKU"] = "LAPTOP";
var products = ruleApi.Eval<List<DynamicDataObject>>("Find Products By SKU", parameters);