Velocity and Custom Objects - Extreme Basics of Syntax/Use Cases | Community
Skip to main content
New Participant
March 17, 2023
Solved

Velocity and Custom Objects - Extreme Basics of Syntax/Use Cases

  • March 17, 2023
  • 1 reply
  • 5875 views

I apologize for all the Velocity posts, but I'm bashing my head against the wall, so I want to get super basic here and explain the issues I am having and make sure I understand the syntax properly.

Here is a junk data image of one of our objects:

CO_example.PNG

We tend to have multiple of these "Customer Profiles" underneath one email - so one "Lead" might actually hold the info for multiple individuals. This starts my rounds of very basic barebones questions:

1) For Custom Objects, to use the #foreach action, is this the correct syntax?

Assuming $individualEmailIdentifier is the field itself and customerProfiles is the object name?

#foreach ($individualEmailIdentifier in $customerProfilesList)

 

2) If I wanted to output ALL the values in the entire Customer Profile for "Age", would the syntax for the output be:

$customerProfilesList.age

OR

$age

 

3) If I wanted to output ONLY values related to one profile in the array, how do I ensure that? I'm assuming I would need something more distinct like a ROW ID or other field? I get using a #foreach, but how do I pull only one set of values instead of all?

 

4) In the case of the below, I get that the "get(0) pulls the 0th value in the Array, but I'm assuming it looks across the entire Customer Profile array regardless of which "profile" it is pulling from? It's pulling the most recent updated data for that particular field?

#if ( $customerProfilesList.get(0).membershipStatus.equals("Member") ) Lead is a Member #else Lead IS NOT a Member #end

 

Basically, the issue across our data boils down to needing to pull multiple values from a specific row and I'm currently not able to effectively do that.

I apologize for the long post and probably basic questions, but I haven't found a lot of basic data on how to get started with Velocity within the context of Custom Objects - or I am looking for the wrong things 🙂 Thank you!!

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 Darshil_Shah1

Make sure you use the correct API name for the custom object list and the custom object fields. You could check the correct API name for the CO list and field when you pull the respective CO field in the email script editor. Alternatively, you could also check Marketo Admin > Custom Object > Select the correct custom object detail page.

 

1. Each custom object record has fields with values in it (e.g., the one you have shown in the snapshot represents one CO record with all its values). When you're looping through the custom object list, the first variable in the loop represents one single custom object record at the nth position (where n is the iteration, or the loop counter). You should not use a CO field name in the foreach loop, rather you should use a loop variable that'd be used the custom object record at the nth iteration. e.g., below:

#foreach($item in $customerProfilesList) ##$item would have the reference of custom object record at the nth iteration in the custom object list.

 

2. If you're accessing the age field for a custom object record in the foreach loop, you should use the loop variable:

$item.age

 

3. You loop through all the custom object records, in the loop-body check if it's the custom object record that you want based on certain fields and if so, output that CO record's fields as required. You could also user the sort method to sort the customobject list based on a field in either descending or ascending order and then print the CO fields of required CO record using the array index.

#set( $sortedList = $sorter.sort($customObjList,"updatedAt:desc") )

 

4. The most recently updated custom object is stored at the top (i.e., the 0th index). A lot of people have a misconception that CO records are sorted based on created date, but actually, records are sorted based on the updated At date in descending order. If you want to pull multiple records' fields you can either sort the CO and query it using the array indexing or loop through the entire CO list and check whether the CO record in the loop iteration is the one that you wish to print the data for using #if condition.

#foreach( $item in $customerProfilesList) ${item.field1}<br/> ${item.field2}<br/> ${item.field3} ##..and so on #end

1 reply

Darshil_Shah1
Darshil_Shah1Accepted solution
Community Manager
March 17, 2023

Make sure you use the correct API name for the custom object list and the custom object fields. You could check the correct API name for the CO list and field when you pull the respective CO field in the email script editor. Alternatively, you could also check Marketo Admin > Custom Object > Select the correct custom object detail page.

 

1. Each custom object record has fields with values in it (e.g., the one you have shown in the snapshot represents one CO record with all its values). When you're looping through the custom object list, the first variable in the loop represents one single custom object record at the nth position (where n is the iteration, or the loop counter). You should not use a CO field name in the foreach loop, rather you should use a loop variable that'd be used the custom object record at the nth iteration. e.g., below:

#foreach($item in $customerProfilesList) ##$item would have the reference of custom object record at the nth iteration in the custom object list.

 

2. If you're accessing the age field for a custom object record in the foreach loop, you should use the loop variable:

$item.age

 

3. You loop through all the custom object records, in the loop-body check if it's the custom object record that you want based on certain fields and if so, output that CO record's fields as required. You could also user the sort method to sort the customobject list based on a field in either descending or ascending order and then print the CO fields of required CO record using the array index.

#set( $sortedList = $sorter.sort($customObjList,"updatedAt:desc") )

 

4. The most recently updated custom object is stored at the top (i.e., the 0th index). A lot of people have a misconception that CO records are sorted based on created date, but actually, records are sorted based on the updated At date in descending order. If you want to pull multiple records' fields you can either sort the CO and query it using the array indexing or loop through the entire CO list and check whether the CO record in the loop iteration is the one that you wish to print the data for using #if condition.

#foreach( $item in $customerProfilesList) ${item.field1}<br/> ${item.field2}<br/> ${item.field3} ##..and so on #end
New Participant
March 17, 2023

Oh wow, that makes SO much more sense - I apparently misconstrued the $item as a placeholder for the field name and NOT the actual use it should be.
One more question if you have time - How would you approach pulling multiple values from a single record?
Like if I have 3 total records, but I want to pull fields from each in conjunction like:

Record A
Field Value A1
Field Value A2

Field Value A3

 

Record B
Field Value B1
Field Value B2

Field Value B3


I understand the foreach can loop through the array on a field basis, but is there a way to basically isolate a complete record (like the screencap I posted) and pull values from that single record?
Maybe I'm dumb but is that what the .get(0) would do? Is that looking on the record level? So if I do

$item.get(0).fieldA $item.get(0).fieldB $item.get(0).fieldC $item.get(1).fieldA $item.get(1).fieldB $item.get(1).fieldC

This would pull each field on the record level?

 

Darshil_Shah1
Community Manager
March 17, 2023

I understand the foreach can loop through the array on a field basis, but is there a way to basically isolate a complete record (like the screencap I posted) and pull values from that single record?
Maybe I'm dumb but is that what the .get(0) would do? Is that looking on the record level? So if I do$item.get(0).fieldA $item.get(0).fieldB $item.get(0).fieldC $item.get(1).fieldA $item.get(1).fieldB $item.get(1).fieldC

This would pull each field on the record level?

 


If you're pulling out the records from the list (i.e., one or multiple CO records), then you could use the index to get the correct CO record using indexes -

The below will return fieldA, fieldB, and fieldC from the first CO record stored in the list (by default CO records are stored by last updated by)

$customerProfilesList.get(0).fieldA

$customerProfilesList.get(0).fieldB

$customerProfilesList.get(0).fieldC

 

The below will return fieldA, fieldB, and fieldC from the second CO record stored in the list 

$customerProfilesList.get(1).fieldA

$customerProfilesList.get(1).fieldB

$customerProfilesList.get(1).fieldC

You should technically use the looping construct instead of quering the random records apart from the first record of the sorted list in most cases.


One more question if you have time - How would you approach pulling multiple values from a single record?
Like if I have 3 total records, but I want to pull fields from each in conjunction like:
Record A
Field Value A1
Field Value A2

Field Value A3

 

Record B
Field Value B1
Field Value B2

Field Value B3

 


When you're looping through the list, in each iteration of the loop, you'd only have access to one CO record (i.e., in the above example, in the first iteration, RecordA would be available in the $item variable, in the next iteration, RecordB would be available in the $item and so on..), so you need not use the indexes as you had to while accessing data directly from the customerProfilesList, you can directly reference the field from the CO record from the loop:

$item.fieldA

$item.fieldB

$item.fieldc

 

I hope this answers your question.