Velocity Script Creating Maps in a Loop | Community
Skip to main content
Nicholas_Sewitz
Level 2
October 10, 2017
Question

Velocity Script Creating Maps in a Loop

  • October 10, 2017
  • 3 replies
  • 3858 views

Hey, I have a velocity script that loops over a custom object, Shows. I want to essentially to create a custom token that if there is only one show in the list does one thing and if there are multiple shows in the list does another thing. I've got that part down, its what happens when there are multiple shows that I am having trouble. The below code will only display one show for $fairMap even though there are 2 in $showList. Ideally if there are multiple shows I want the script to iterate over each show that meets the criterea and output like this (without me having to hardcode obviously [0][1][2][etc]:

I am pleased to share that the online preview of the below fairs:<br><br>

Show Name: $showName[0]<br>

URL: $showURL[0]<br><br>

Show Name: $showName[1]<br>

URL: $showURL[1]<br><br>

Show Name: $showName[2]<br>

URL: $showURL[2]<br><br>

This is what I have so far. I am trying to store each show name/url in the $fairmap map.

##Initialize

#set ( $showName = false )

#set ( $x = $date.calendar )

#foreach($show in $Show__cList)

    #set ( $fairPreviewDay = $show.Fair_Autopublish_At__c )

    #set ( $fair_preview_date_object = ${convert.parseDate(${show.Fair_Autopublish_At__c}, 'yyyy-MM-dd')} )

    #set ( $fair_preview_date_days = ${date.whenIs($fair_preview_date_object).days} )

    #set ( $showTriggeredDate = $show.Send_First_Triggered_Email__c )            

    #set ( $showURL = $show.CMS_URL__c )

    #set ( $showListSize = $Show__cList.size() )

   

   

    #if ( $show.Fair_Name__c )

    #set ($showFairName = $show.Fair_Name__c )

    #else

    #set ($showFairName = $show.Name)

    #end

   

    #if ( $showTriggeredDate && $fair_preview_date_days == 0 ) 

      #if ( $showListSize > 1 )

      #set( $FairInfoMap = { 

          "Fair Name: " : $showFairName,

          "URL: " : $showURL } )

      #elseif( $showListSize == 1 )

      I am pleased to share that the official online preview of $showFairName  has launched. Please check out your booth in the CMS: $showURL

      #end

    #end  

#end

#if ( $showListSize > 1 )

I am pleased to share that the online preview of the below fairs have launched:<br><br>

    $FairInfoMap

#end   

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

3 replies

Josh_Hill13
Level 10
October 10, 2017

@Sanford Whiteman​

SanfordWhiteman
Level 10
October 10, 2017

Skipping over the parts of your code that aren't relevant, you want to add your matching shows to another list. Then iterate over that list.

#set( $showsToOutput = [] )

#foreach( $show in $Show__cList ) 

#set( $showTriggeredDate = $show.Send_First_Triggered_Email__c )              

#set( $showURL = $show.CMS_URL__c )

#set( $showFairName = $show.Fair_Name__c )

#if( $showTriggeredDate )

#set( $showable = {

  "Fair Name" : $showFairName,

  "URL" : $showURL

})

#set( $tmp = $showsToOutput.add($showable) )

#end

#end

#if( $showsToOutput.size() > 1 )

I am pleased to share that the online preview of the below fairs have launched:<br><br> 

#foreach( $showable in $showsToOutput )

Name: ${showable["Fair Name"]}<br>

URL: ${showable["URL"]}<br>

#end

#else

I am pleased to share that the official online preview of ${showsToOutput[0]["Fair Name"]} has launched. Please check out your booth in the CMS: ${showsToOutput[0]["URL"]}<br>

#end

Nicholas_Sewitz
Level 2
October 11, 2017

Thanks Sanford, works perfectly. Out of curiosity, why do you have to assign the below to tmp. can you not just ".add"

  1. #set( $tmp = $showsToOutput.add($showable) ) 
SanfordWhiteman
Level 10
October 11, 2017

Velocity statements require a left and right side.