Tokens Pulling Multiple Custom Object Information

Juli_Cummins
Level 1

Tokens Pulling Multiple Custom Object Information

We are noticing an issue where the tokens used to pull a badgeID are pulling in data from the custom object from the two past years rather than just the most recent year as defined in the token. 

I've tried to use .get(0) to pull only the most recent entry in the custom object, but I haven't been successful. Below is the code that I have tried to do this based on reading other posts and trying to piece something together.

##begin lopping through Contact List

    #foreach($event in $Contact_Event__cList)

   

        ##retrieve event name and registration type

        #set ($name = ${Contact_Event__cList.get(0).Event_Name__c})

        #set ($regType = ${event.Registration_Type__c})

       

       ##name: $name <br>

        ##regType: $regType <br>

       

        ##check if $year matches designated year

         #if( ($name == "2017") && ($regType == "EO") || ($regType == "P") )

         

                ${event.Badge_ID__c}

               

        ##end if statement

        #end

    ##close the loop

    #end

Below is how the custom object is set up:

pastedImage_3.png

Thank you!

Juli

1 REPLY 1
SanfordWhiteman
Level 10 - Community Moderator

Re: Tokens Pulling Multiple Custom Object Information

I've tried to use .get(0) to pull only the most recent entry in the custom object, but I haven't been successful. Thank you!

In what way haven't you been successful?  Please be specific about the code you tried (which isn't in your pasted snippet) and the expected/observed behavior. If the most recent entry isn't always the single one you want, then [0] wouldn't work anyway.

I would advise initially sorting the whole list, descending, by the relevant Date field. You can see a ton of examples of this in my posts on Velocity (both here and and on http://blog.teknkl.com/tag/velocity).

Once you presort, your #foreach loop will be stepping back in time. Thus, when you find a matching object, that'll be the newest matching object. You can output it (or save it to another variable) and #break.