Scalar-function rule
Introduction
Casewhere rule is not only about boolean predicates. You can use rule to implement a scalar function that does some logic and returns a value. It is always a good practice to extract computing logic in workflows into rules that can be reused in different places.
Rule syntax
Keywords
You can use the following keywords to work with scalar-function rules:
Name | Type | Description |
---|---|---|
@users | DslUser | The user object in context. Read access rule for more information. |
@parameters | DynamicDataObject | You can access attributes using the “dot” operator. For example, “@parameters.address”. |
@apiFactory | IManagedApiFactory | To construct Casewhere API objects. Read here for the full list of available Apis. |
Examples
Example 1
Assuming you have a rule "Convert Meter To Centimeter" that converts centimeter to meter:
@parameters.meterValue * 100
You can evaluate the rule in a workflow scripted activity using RuleApi
:
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
Assuming you need to implement a function to query products by SKU and you want to reuse it in many places. You can create a rule "Find Products By SKU" like below:
#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;
You can evaluate the rule in a workflow scripted activity using RuleApi:
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);