Return Values

This post follows on from the general concepts in Logs, Errors and Outputs.

It shows how Streamscript actions may return both primitive types and record/collections types without needing hard coded Apex. To do this, Streamscript marshals all data into pre-defined output variables based on the nature of that data.

Pull from automatic output values...

...or push into manually assigned variables

Expression Return

Any expression may appear after the return keyword. Streamscript automaticallys sets the action's output variable (num, bool, text, list, record, records) according to the data type.

# Streamscript (inverse exchange rate)
$result = Json-Decode $http.body
return 1 / $result.rates.USD

Variable Return

Any script variable may be returned as a flow resource, including records and collections. For records and collections, the variable name must indicate its type, eg: return $Account

Streamscript Variable Flow Resource
return $num Number
return $bool Boolean
return $text Text
return $texts Text Collection
return $Account # map Record
return $Contact # list of maps Record Collection

We encourage voting for this idea: Provide the ability to use Maps in Flows. As a workaround without maps, you can use the map-like aspects of records in many scenarios. Assign any dates, times, datetimes or blobs to a field on a record or a collection of records as needed.

This example retrieves a PDF invoice. The binary blob is stored on the Document Body field, then it returns the $Document record so that Flow can insert it.

# get pdf invoice, base64 encoded
$url = 'www.ups.com/media/en/invoice.pdf'
$pdf = Http-Get $url -base64

# new record
$Document = New-Document {
    Name = 'inv.pdf'
    Body = $pdf.body
}

# output as SObject
return $Document

Parameterised Return

Instead of one variable, you may simultaneously return any/all of the above variables. This example returns an Opportunity header and a list of OpportunityLineItem at the same time:

# shopping cart
$Opportunity = New-Opportunity
$Opportunity.Name = $cart.shopping_cart_name

# each item in cart
$OpportunityLineItem = []
foreach ($item in $cart.shopping_cart_items) {...}
return -record $Opportunity -records $OpportunityLineItem

Another example - issue a POST request and return the HTTP response attributes to flow:

# Streamscript
$Http = Http-Post 'callout:httpbin/post'
$body = $Http.body
$status = $Http.status
$headers = $Http.headers.values()
return -num $status -text $body -texts $headers

Null Return

Handing back a result from a script is completely optional. Scripts that return nothing are said to return null. There is no value, but the script ends and flow proceeds to the next element.

# Streamscript
if ($record_already_exists) return
...

Implied Return

One-line scripts automatically return the result of the expression. Streamscript will set the output variable (num, bool, text, list, record, records) according to any result data type.

# Streamscript (returns boolean)
BusinessHours-IsWithin 'Eastern Standard Time' {!$Flow.InterviewStartTime}

 

That sums up the ways to pass data from Streamscript back into Flow.

 

Getting started with Streamscript
Install from the Salesforce AppExchange
Direct install URL: /packaging/installPackage.apexp?p0=04t7F000005N1wy