Rule Engine Loops

In order to perform some actions on every element of a collection, PackageBee's rule language includes a for/end loop that can be used to iterate over all order items in an order. No special construct is needed to iterate over orders because PackageBee's rule engine applies rules to each order separately.

The for and end keywords of the for/end construct are inserted automatically in the GUI Editor but need to be specified in the rule source code in the Advanced Editor:

for items in collection

For example,

for order_items in order
   # loop through each item in an order and 
   # test against conditions in this block 

Assuming that we have the following sales order:

PackageBee - Sales order 1

and that we would like to add the current year to each SKU, we would be able to accomplish this with the following rule that loops over all items in a sales order and concatenates the current SKU with the string "-2020":

rule "Add year at the end of each SKU"
for order_items in order
set order_item.sku to order_item.sku & "-2020"

After the above rule will be executed on the order, the SKUs in it will be changed accordingly:

PackageBee - Sales order 2

It should be noted that unlike traditional looping in most programming languages where the different parameters used in a loop need to be explicitly defined, PackageBee's for/end loop does not require users to specify a starting point or an increment parameter, so it is in that sense more similar to JavaScript's for/of loop:

//display array elements

let array = [1,2,3,4,5];
for(el of array)

or JavaScript's for/in loop, which is used with objects:

//display object values 

let obj = {"a":1,"b":2,"c":3};
for(key in obj)
Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request



Please sign in to leave a comment.