SOLVED

A basic foreach issue

Go to solution
Highlighted
Level 10 - Community Moderator

Re: A basic foreach issue

  • You don't need to pre-set attendees in the details Map, though it's nicely informative if you do.
  • You aren't using this variable: 
      #set( $attendees = $currentRegistration[1] )​
  • Splitting on just ; is fine, you don't need to put it in a character class [;] as it's not reserved
Highlighted
Level 5

Re: A basic foreach issue


@SanfordWhiteman wrote:
  • You don't need to pre-set attendees in the details Map, though it's nicely informative if you do.
  • You aren't using this variable: 
      #set( $attendees = $currentRegistration[1] )​
  • Splitting on just ; is fine, you don't need to put it in a character class [;] as it's not reserved

  • I like being informative, especially in a language I'm not super familiar with.  It helps me understand my work later.  That being said, I take your point about it not being needed.
  • True enough.  That line can go
  • Gotcha.  Makes perfect sense.

Now time to do a gloriously formatted table for output, and I'm most of the way there! 🙂

 

Thanks so much for all your help @SanfordWhiteman .

 

Cheers

Jo

Highlighted
Level 5

Re: A basic foreach issue

Hmm... so my code now looks like this (and seems to be working)

#set( $allSeminarDetails = {
  "ABC" : {
    "blurb":"Postman Pat",
    "date":"24 Aug",
    "time":"10:30",
    "attendees":0,
    "village":"somewhere"
  },
  "CBA" : {
    "blurb":"Bob the builder",
    "date":"14 Aug",
    "time":"10:30",
    "attendees":0,
    "village":"somewhere else"
  }
  
} )  
#set( $currentSeminarList = $lead.metlifecompetitiondatas2 )
#set( $currentSeminarCodes = $currentSeminarList.split("[|]") )
## this list will hold matched seminars
#set( $currentSeminarDetails = [] )
## find matching seminar in master list
#foreach( $code in $currentSeminarCodes )
  #set( $currentRegistration = $code.split(";") )
  #set( $villageCode = $currentRegistration[0] )
  #set( $aSeminar = $allSeminarDetails[$villageCode]) 
  #set( $aSeminar.attendees = $currentRegistration[1] )
  #set( $void = $currentSeminarDetails.add( $aSeminar ) )
  #end
## iterate over matches only
#foreach( $seminar in $currentSeminarDetails )
  <table style="table-layout:fixed; width:100%; background:#eeeeee">
  <tr>
  <td style="width:30%;">Date</td><td style="">${seminar.date}</td>
  </tr>
  <tr>
  <td>Time</td><td style="">${seminar.time}</td>
  </tr>
  <tr>
  <td>Village</td><td style="">${seminar.village}</td>
  </tr>
  <tr>
  <td>Details</td><td style="">${seminar.blurb}</td>
  </tr>
  <tr>
  <td>Attendees</td><td style="">${seminar.attendees}</td>
  </tr>
  </table>  
  <br>
  #end

But I get the following error when I try to either approve the email or send a sample.

Validation Error approving LG - 202007 - RRS - CORE.01 - Registration Acknowledgement —  <div>An error occurred when procesing the email Rendered_Email_Velocity_Error_Area_?! </div> <p>Error invoking method 'get(java.lang.Integer)' in [Ljava.lang.String; near</p> <div><pre >?</pre></div>

 

I have made sure I don't have the velocity in my text version of the email.  What should I look for next?

Cheers

Jo

Highlighted
Level 10 - Community Moderator

Re: A basic foreach issue

You haven't accounted for the nulls that happen when the lead field is empty (which is the case when you approve).

Highlighted
Level 5

Re: A basic foreach issue

@SanfordWhiteman ,

gotcha.  I presume the easiest way to do this is to wrap all the code after this

#set( $currentSeminarList = $lead.metlifecompetitiondatas2 )

with this

#if( $currentSeminarList != "" )
  All the doing code 
  #end

It seems to behave, but is that the best practice approach?

 

Highlighted
Level 10 - Community Moderator

Re: A basic foreach issue

To be most proper:

 

#if( $display.alt($currentSeminarList,"").isEmpty() )

 

While in practice a lead field can only be empty, not null, this isn't the case for CO fields and you should be ready for both cases. 

Highlighted
Level 5

Re: A basic foreach issue

Then most proper we shall be.