SOLVED

Looping through Custom Objects

Go to solution
mkto101
Level 1

Looping through Custom Objects

Hi All, I have a script that I just can't get working, I am using it to learn, but any help would be appreciated. 

 

I have a custom Object called car with the following fields:

Make

Model

Year 

Vin (Dedupe)

EmailLinkToPerson (Dedupe)

 

I am trying to write a foreach loop to show me all records that has a car associated with it.  this is my code but it keeps returning 0 when there are 35 records. 

 

#set ($count = 0)

#foreach ($item.emailLinkToPerson in $car_cList)
  #set ($count = $count + 1)
#end

number of Cars linked to a person:  $count  





to test this I have added the following token snippet to an email draft (possible to use LP instead? or whats a better why to test scripts)

{{my.Cars_token}}

1 ACCEPTED SOLUTION

Accepted Solutions
Jo_Pitts1
Level 10 - Community Advisor

Re: Looping through Custom Objects

@mkto101 ,

by default, Marketo only returns the first 10 rows for a given CO for a given person.

To change this, go to Admin | Email, scroll down, and then change this setting:

Jo_Pitts1_0-1713983785967.png

 

Cheers

Jo

 

View solution in original post

6 REPLIES 6
Jo_Pitts1
Level 10 - Community Advisor

Re: Looping through Custom Objects

@mkto101 ,

I've edited your post to make the code more readable.  Here is a link to my blog post on how/why to do this: https://nation.marketo.com/t5/product-blogs/posting-code-in-discussions/ba-p/341001

 

When posting in future, please make sure to format code for readability please.

 

You can't use the token on an LP.  In Marketo, Velocity is only used in Emails.

 

So, to your code:

  1. Have you made sure a field in your custom object is checked in the RHS tree?
  2. Your loop is weird.  Try something more like this:
    #set ($count = 0)
    
    #foreach ($car in $car_cList)
      #set( $count = $math.add($count,1) )
    #end
    
    number of Cars linked to a person:  $count  ​

    Marketo inherently understands the link between a person and their custom objects (if you've set things up properly).

  3. I know you're doing this to learn, so while there are other ways of getting a count of the items in a list.  However, I'll not dig into those as you look to expand your horizons.
  4. I used $math.add rather than a + sign.  Check out this post for why I did this: https://nation.marketo.com/t5/product-blogs/the-only-time-you-should-use-the-sign-in-velocity-instea...
  5. For your test record, have you confirmed that the 35 records are actually linked to them, but going into their record | Custom Objects | Car

Cheers

Jo

 

mkto101
Level 1

Re: Looping through Custom Objects

Hi Jo, 

 

thank you so much for the pointers and the advice along the way. The total count is still off, this is really a weird issue,  so this person has over 35 cars assigned to him, and the loop now only shows 10. I also tried to follow @SanfordWhiteman advice and wrap in a conditional statement and use List.size instead of a loop and this only gives me 10 record for the person. here is the 2 codes I tried:

 

 

#set ($count = 0)

#if( $car_cList.size() != 0)
  #set( $count = $car_cList.size() )
#end

$count

 

 

the above only returns 10 when there are over 100 records


 

 

#set ($count = 0)

#foreach ($car in $car_cList)
  #set( $count = $math.add($count,1) )
#end

number of Cars linked to a person:  $count  ​

 

 

 

The Above code is the one that you suggested, Joe, and this only returns the first 10 records. Is there a hard limit of 10?  it seems odd that the loop would break.

 

I have ensured that all items in the RHS tree are checked off and when I go to the persons -> Custom Object -> Cars I do see all the cars assigned, and there are over 100 records, I also created a smart list for spot-checking and I also see a long list with over 100 records, but the code only returns the first 10 records.

Thanks for all your help!

SanfordWhiteman
Level 10 - Community Moderator

Re: Looping through Custom Objects

Jo alluded to this, but why are you looping at all? No need for that, and it wastes resources. List.size() returns the count of all items.

Now if you want to get a count of list items that match some particular conditions, then you need to update a separate counter after checking #if() some conditions match.

mkto101
Level 1

Re: Looping through Custom Objects

hey @SanfordWhiteman 

 

that's a solid advice. Thanks for pointing that out. I rather avoid loops where I can, so this is gold! However, I am still not getting an accurate count. I have multiple persons set up with 5,8,9 and 10 cars all of which are showing accurate count, but the persons with 11+ cars are all defaulting to 10 cars rather than showing the actual count. do you know if there is a hard limit of 10 seems bizarre. It wouldn't reflect the count you see under the person -> Custom Object - > Cars.

 

With one of the Persons it was showing 5 (accurate count), and I kept adding 1 car at the time to the breaking point, which seems to be greater than 10 records per person. Anything you can share in this regard is appreciated. 

 

TVMIA!

Jo_Pitts1
Level 10 - Community Advisor

Re: Looping through Custom Objects

@mkto101 ,

by default, Marketo only returns the first 10 rows for a given CO for a given person.

To change this, go to Admin | Email, scroll down, and then change this setting:

Jo_Pitts1_0-1713983785967.png

 

Cheers

Jo

 

mkto101
Level 1

Re: Looping through Custom Objects

you are the man! thank you!