Guide - Script tips
Range operator
Quickly create a list of items representing a contiguous range.
$grades = 'A'..'F' # A, B, C, D, E, F $months = 1..12 # 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
Negative Index
Use negative indexing to access elements from the end of a list by counting back: eg -1, -2...
$queue = ['Alice', 'Bob', 'James'] $last = $queue.get($queue.size - 1) # James $last = $queue.-1 # also James
Like operator
Checks for a partial text match (as opposed to the == operator which requires an exact match)
'Salesforce.com' like '%force.com' # true
Optional parentheses
Parentheses are optional when calling a method that takes no parameters.
$length = 'hello'.length() # this works $length = 'hello'.length # this also works
Ternary operator
Shorthand for condition { value_if_true } else { value_if_false }
$jackpot = $prize > 10000 ? 'big' : 'small'
Template
Use templates to combine text and variables cleanly without concatenation operators.
$concat = 'Hello' + ' ' + {!FirstName} + ' ' + {!LastName} # quoted text
$template = `Hello {!FirstName} {!LastName}` # template text
Choice method
The choice method picks a friendly label for 0 items, 1 item, or 2+ items, based on the argument:
# Your cart contains 3 items
$items = ['Pens', 'Staples', 'Toner']
$message = `Your cart contains $items.choice('no items', 'one item', '% items')`
In this example $items references the list. The % symbol represents the items count.