The set action assigns a value to a field. It’s the most common action in BGL.
set order.shipping_carrier_name to "UPS"
The pattern is set <target> to <expression>. The target is what you’re changing; the expression is the value to put there.
What you can set
-
A field on the model:
set order.notes to "..."orset order_line.sku to "..." -
A nested field:
set order.shipping_address.country to "US" -
A custom field:
set order.custom.tier to "gold" -
A session variable:
set $total to 0.0(see Variables)
What you can set to
The right-hand side of set ... to ... is any BGL expression. That includes:
Literals:
set order.shipping_carrier_name to "UPS"
set order.shipping_amount to 0.0
set order.custom.is_gift to true
set order.custom.scheduled_for to @tomorrow
Other fields:
set order.billing_address.country to order.shipping_address.country
set order_line.unit_cost to order_line.unit_price
Math:
set order.tax_amount to order.subtotal * 0.08
set order.shipping_amount to order.subtotal * 0.05
String concatenation (with &):
set order.notes to "Customer: " & order.customer_email
set order.custom.recipient_name to order.shipping_address.first_name & " " & order.shipping_address.last_name
Type-cast values:
set order.custom.priority to order.custom.priority_text as number
set order.notes to order_line.qty_ordered as text & " items"
See Data Types for casting and Strings and Regex for string operations.
Clearing a value
To clear a field, set it to blank:
set order.shipping_amount to blank
set order.notes to blank
blank is BGL’s way of saying no value (null). The same keyword works for any field type.
Setting based on a condition
Wrap a set in a when block to apply it conditionally:
when
order.subtotal > 500.0
then
set order.custom.tier to "gold"
end
For if-this-then-A-else-B logic, use two when blocks — BGL doesn’t have an inline conditional operator:
when
order.subtotal > 100.0
then
set order.custom.tier to "premium"
end
when
order.subtotal <= 100.0
then
set order.custom.tier to "standard"
end
(Or simpler: set a default first, then override.)
set order.custom.tier to "standard"
when
order.subtotal > 100.0
then
set order.custom.tier to "premium"
end
The second block runs after the first, so the override wins when the condition matches.
Setting collection items
Inside a for loop, you can set fields on each iterated item:
for order_lines in order
set order_line.custom.processed to true
end
See Loops for the iteration syntax.
Setting in nested actions
Some actions create new items, and the set calls inside configure that new item:
add 1 item to order_lines in order
set item.sku to "GIFT-WRAP"
set item.unit_price to 0.0
end
Here item refers to the item being added. See the Action Reference for add, accumulate, and other actions that work this way.
Limitations to know about
BGL doesn’t have built-in functions for common transformations. There’s no trim, uppercase, substring, abs, round, min, max, etc. To do those things you’ll need:
-
String manipulation — use regex via
replace /.../ with "..." in field. See Strings and Regex. -
Math — use arithmetic operators. For things like absolute value or rounding, work it out with conditional
sets.
If you find yourself wanting a function that doesn’t exist, the workaround is usually a session variable populated by when blocks. See Variables.
Comments
Please sign in to leave a comment.