Extended Personalization Flow: Subscriptions

Deliver a list of subscriptions related to the current visitor’s Lead record.

Custom object

This example uses a custom object Newsletter Subscription related to Lead by a lookup field. For our purposes there are two relevant fields:

  • Name: the unique name of the subscription.

  • IsSubscribed__c: checkbox that determines whether the Lead is subscribed or not.

Create Flow

Create a new Autolaunched Flow (No Trigger).

Flow Resources

Add the following resources:

  • The VisitorRecord and VisitorData variables for input and output.

  • Two Apex-defined variables for temporary data, also using the send2crm__GenericFlowData class. These should be single variables, not Collections:

    • TemporaryDataText

    • TemporaryDataCheckbox

  • A LoopIndex Number variable with zero decimal places.

  • Two Text formula resources for the property accessors:

    • NameAccessor: "Subs:" + TEXT({!LoopIndex}) + ":Name"

    • IsSubscribedAccessor: "Subs:" + TEXT({!LoopIndex}) + ":IsSubscribed"

Flow Elements

Add a Get Records element to load all Newsletter Subscription records related to the visitor’s Lead record.

If visitors are not always related to a Lead, you should add a Decision element to end the Flow when applicable.

Add a Loop element to iterate the result of Get Records.

Inside the loop, add an Assignment element. Assign the following variables:

  • TemporaryDataText.accessor: NameAccessor (formula)

  • TemporaryDataText.valueAsString: Current loop item (record) > Name

  • VisitorData: add TemporaryDataText

  • TemporaryDataCheckbox.accessor: IsSubscribedAccessor (formula)

  • TemporaryDataCheckbox.valueAsBoolean: Current loop item (record) > IsSubscribed__c

  • VisitorData: add TemporaryDataCheckbox

  • LoopIndex: add 1

All assignments can be made within the single element. However the order matters, e.g. you need to populate the temporary variables before adding to the VisitorData collection. LoopIndex should be last.

While not strictly necessary to create two temporary data variables, it makes using different data types easier. If you assign both valueAsBoolean and valueAsString, only the first will be applied by Send2CRM when applying personalization.

Debug Flow

Debug the Flow using a Send2CRM Visitor record that is related to a Lead with at least one Newsletter Subscription record. In the assignment details, you should see something like this (formatted here for easier reading):

{!VisitorData} = "[ GenericFlowData : { "valueAsString" : "Financial Newsletter", "accessor" : "Subs:0:Name" }, GenericFlowData : { "valueAsBoolean" : false, "accessor" : "Subs:0:Name" }, GenericFlowData : { "valueAsString" : "Sales Newsletter", "accessor" : "Subs:1:Name" }, GenericFlowData : { "valueAsBoolean" : true, "accessor" : "Subs:1:Name" } ]"

You can now see how the LoopIndex and formula resources allowed us to generate suitable accessor names. Send2CRM will automatically convert this to a nested structure with correct data types when generating the JSON data for the visitor response:

"data": { "Subs": [ { "Name": "Financial Newsletter", "IsSubscribed": false }, { "Name": "Sales Newsletter", "IsSubscribed": true } ] }

This data is return to the website client and can be used as required.

let subscriptionData = send2crm.analytics?.visitor.data.Subs; if (Array.isArray(subscriptionData)) { for (let sub of subscriptionData) { console.log('Visitor is ' + (sub.IsSubscribed ? '' : 'NOT ') + 'subscribed to ' + sub.Name); } }