Use Velocity script to display dynamic content from Marketo custom field | Community
Skip to main content
Level 2
November 8, 2023
Solved

Use Velocity script to display dynamic content from Marketo custom field

  • November 8, 2023
  • 1 reply
  • 2464 views

Hi Team,

 

I need to use Velocity Script to show multiple product information from the Abandoned Cart Custom field in an email. The JSON object below has been stored in a Marketo custom field. Usually, we store cart values in a custom object, but in this case, they need to be stored in a custom field. I'm having difficulty converting the JSON data into a foreach loop.

@sanfordwhiteman @darshil_shah1 Could you please take a look at this and provide your insights?

{ "abandoned_cart": { "11": { "product_name_1": "ABCS", "product_image_1": "3282.jpg", "catalog_1": "960-13", "quantity_1": "3", "unit_1": "EA", "tabl_value_2": "18 Mars 2019" }, "13": { "product_name_1": "BCSDA", "product_image_1": "33282.jpg", "catalog_1": "9610-1", "quantity_1": "3", "unit_1": "IN", "tabl_value_2": "04 avril 2019" }, "15": { "product_name_1": "RTYU", "product_image_1": "282.jpg", "catalog_1": "6410-3", "quantity_1": "3", "unit_1": "US", "tabl_value_2": "15-Avril-2019" } } }My velocity script: #if( $lead.abandonedcart.isEmpty() ) #set( $lead.abandonedcart = '[]' ) #else #set( $CartDetails = '#set( $CartDetails = ' + $lead.abandonedcart + ' )' ) #evaluate( $CartDetails ) ## print $CartDetails #end

Thanks,

Karthik

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by SanfordWhiteman

Iterating over a Map’s entries (entries = keys and values):

 

#foreach( $entry in $CartDetails.abandoned_cart.entrySet() ) #set( $key = $entry.getKey() ) #set( $value = $entry.getValue() ) ## key is your object's numeric id key: $key product_name_1: $value.product_name_1 product_image_1: $value.product_image_1 #end

 

 

1 reply

SanfordWhiteman
Level 10
November 8, 2023

You haven’t provided enough detail.

 

If that JSON is stored in a lead field, and that field is named $lead.abandonedcart in Velocity, then your code is correct for parsing the JSON into a live Java Map object with a single top-level property abandoned_cart.

 

However, you’re talking about a #foreach and I don’t know what you’re expecting to iterate. Keys? Values? Entries?

Level 2
November 8, 2023

Yes, as you mentioned, the JSON is stored in the Lead Abandoned Cart field. I am anticipating the headers and values to be presented like below. I believe that a Foreach loop would be return all values from JSON.

 

Please let me know if you need more information.

Expected output:

product_name_1product_image_1catalog_1quantity_1unit_1
ABCS3282.jpg960-133EA
BCSDA33282.jpg9610-13IN

RTYU

282.jpg6410-33

US

SanfordWhiteman
SanfordWhitemanAccepted solution
Level 10
November 8, 2023

Iterating over a Map’s entries (entries = keys and values):

 

#foreach( $entry in $CartDetails.abandoned_cart.entrySet() ) #set( $key = $entry.getKey() ) #set( $value = $entry.getValue() ) ## key is your object's numeric id key: $key product_name_1: $value.product_name_1 product_image_1: $value.product_image_1 #end