Only top-level, ‘var’-declared variables (or equivalent) are returned by FlowBoost

In the recent post on adding scores in FlowBoost I purposely left out the var statement when declaring variables to be returned:

I decided to leave out the var keyword because you need a property of the global object, and that happens automatically if you omit var. Keeps the request payload a little shorter.

See, with a Standard mode FlowBoost call, only top-level properties are returned in the FlowBoost response. Both var personScore = and personScore = create a top-level property. But let personScore = and const personScore = don’t.

Strategically choosing between var and let/const avoids cluttering your JSON response with stuff Marketo won’t care about. Here, emailAddress and emailParts (in effect temporary variables) are declared with let, while emailAddressLowerCase and emailDomain are both vars:

let emailAddress = {{lead.Email Address}};
let emailParts = FBUtil.string.partsFromEmail(emailAddress);
var emailAddressLowerCase = emailAddress.toLowerCase();
var emailDomain = emailParts.domain;
FlowBoost JS payload showing let and var

The JSON response will only include the 2 declared with var:

   "emailAddressLowerCase" : "",
   "emailDomain" : ""
FlowBoost JSON response

Why FlowBoost works this way

FlowBoost runs your code in a secure JavaScript sandbox, passing it a context, or contextified object, that serves as the top-level object for all the code that runs in the sandbox. The context is a vanilla JavaScript object ({}).

After your code finishes— in Standard authoring mode, it’s different in Pro — any newly added properties of the context object are sent in the JSON response.

It’s sort of a happy accident that only var and an undecorated variable declaration create top-level properties . It wouldn’t be so bad for FlowBoost’s purposes if let and const also did this (you could just ignore the chaff in the response payload). But these newer statements deliberately don’t touch the global object. So we get to manage payload sizes as a result.