Script Tips

Seven of our favourite script tips - some old, some new, some borrowed.

 

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

 Get elements from the end of the list instead of the start 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 is exact)

'Salesforce.com' like '%force.com'    # true

 

Optional parentheses

 Not needed when the method has 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

 Combine text and data without the distraction of operators and formulas.

$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')`Try

 In this example $items references the list. The % symbol represents the items count.

 

 

Getting started with Streamscript
Install from the Salesforce AppExchange
Package install link: /packaging/installPackage.apexp?p0=04tGA000005VVeA