Variables

Karl Falconer · Updated

A variable lets you compute a value once and reuse it later in the same rule. Variables in BGL start with $$total, $category, $is_urgent. They live for the lifetime of one rule’s execution.

rule "Categorize order"
  set $category to "standard"

  when
    order.subtotal > 500.0
  then
    set $category to "premium"
  end

  set order.custom.tier to $category
end

The first set declares $category with the value "standard". The when block can override it. After both blocks run, the final set order.custom.tier to $category writes whatever value $category ended up with onto the order.

If you’ve used a spreadsheet: A $variable is like a named cell that holds a value while a single calculation runs. Other rules can’t see it.

Declaring a variable

There’s no separate “declare” step — the first time you set $name to ..., the variable comes into being and BGL infers its type from the value:

set $total to 0.0          # number (0.0 is numeric)
set $category to "premium" # text
set $is_urgent to true     # boolean
set $deadline to @now      # date

Once a variable’s type is set, you can only assign values of the same type to it (or use as number/text/date/boolean to cast).

Using a variable

Anywhere an expression can go — in a condition, on the right side of a set, inside a calculation:

when $total > 100 then
  set order.notes to "High value: $" & $total as text
end

Scope: one rule, one execution

Variables are scoped to a single run of a single rule. They can’t be shared between rules — every rule starts with a clean slate. To pass information between rules, write it to a model field instead.

# Rule A — sets a custom field
set order.custom.flagged to true

# Rule B — reads the same custom field
when order.custom.flagged then ... end

Within one rule, variables persist across when blocks:

rule "Calculate adjusted total"
  set $multiplier to 1.0

  when order.custom.has_discount then
    set $multiplier to 0.9
  end

  when order.custom.is_priority then
    set $multiplier to $multiplier * 1.1
  end

  set order.custom.adjusted_total to order.subtotal * $multiplier
end

The order matters — $multiplier accumulates updates from each when block in sequence.

Common patterns

Accumulating across when blocks:

set $tags to ""

when order.custom.is_gift then
  set $tags to $tags & "gift,"
end

when order.custom.is_priority then
  set $tags to $tags & "priority,"
end

set order.custom.tag_list to $tags

Using a default with overrides:

set $tier to "standard"

when order.subtotal > 100.0 then set $tier to "premium" end
when order.subtotal > 1000.0 then set $tier to "vip" end

set order.custom.tier to $tier

Storing a result of an accumulate block:

accumulate order_lines in order
  $line_total: sum order_line.unit_price * order_line.qty_ordered
end

when $line_total > 500.0 then
  set order.custom.tier to "high-value"
end

(See Accumulators.)

Clearing a variable

set $name to blank resets the variable. After that, the variable still exists but has no value.

set $temp to "scratch"
# ... use $temp ...
set $temp to blank

You rarely need to clear a variable — when the rule finishes, all variables are discarded automatically.

Naming tips

  • Use meaningful names. $total_value is better than $x.
  • Use snake_case. $line_total, not $lineTotal.
  • Avoid shadowing field names. A variable called $subtotal and a field called order.subtotal are unrelated, but mentioning them next to each other is confusing.
Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.