SOLVED

Re: Picking first item off an array from semi colon separated list using Velocity

Go to solution
Jon_Wright
Level 4

Picking first item off an array from semi colon separated list using Velocity

Hi - I'm going a bit mad doing something which I thought would be pretty simple. I have a ; separated field on the lead record and I just want to get the first matching record from an array using the 'sessionKey' so I can use the values in an email block.

 

I've tried multiple things but here's my last attempt which hits a

'Invocation of method 'contains' in class java.lang.String threw exception java.lang.NullPointerException near'

error.

 

 

#set( $allSessions = 
[{
  	"sessionKey": "Session1",
    "sessionName":"Session 1 Name",
    "imageURL":"example.com/image1"
},
{
  	"sessionKey": "Session2",
    "sessionName":"Session 2 Name",
    "imageURL":"example.com/image2"
}
])
#foreach( $session in $allSessions)
#if( $lead.recommendedSession.contains($allSessions.sessionKey))
#set( $recommendedSession = $session )   
    #break          
#end
#end
$recommendedSession.sessionKey
$recommendedSession.sessionName
$recommendedSession.imageURL

 

I think I need to split my list and do an exact match but when I tried that I didn't get the error but I didn't get any values either (doubled checked lead record I was testing with etc)

@SanfordWhiteman - are you able to help?

2 ACCEPTED SOLUTIONS

Accepted Solutions
Salytics
Level 3

Re: Picking first item off an array from semi colon separated list using Velocity

Hi Jon,

Since your foreach variable is $session, could you try changing

#if( $lead.recommendedSession.contains($allSessions.sessionKey))


to

#if( $lead.recommendedSession.contains($session.sessionKey))


Hopefully it's as easy as that.

View solution in original post

SanfordWhiteman
Level 10 - Community Moderator

Re: Picking first item off an array from semi colon separated list using Velocity

@Salytics is definitely right that you're ref'ing the wrong variable ($allSessions where you should have $session).

 

But that's not all. You do need to split the semicolon-delimited string as well, as you suggested. Using contains() on a delimited string is a really bad idea (think about "Session1" and "Session12").

#set( $allSessions = 
[{
  "sessionKey": "Session1",
  "sessionName":"Session 1 Name",
  "imageURL":"example.com/image1"
},
{
  "sessionKey": "Session2",
  "sessionName":"Session 2 Name",
  "imageURL":"example.com/image2"
}
])
#set( $leadSessionKeys = $lead.recommendedSession.split(";") )
#foreach( $session in $allSessions)
#if( $leadSessionKeys.contains($session.sessionKey))
#set( $firstRecommendedSession = $session )   
#break          
#end
#end
${firstRecommendedSession.sessionKey}
${firstRecommendedSession.sessionName}
${firstRecommendedSession.imageURL}

 

View solution in original post

4 REPLIES 4
Salytics
Level 3

Re: Picking first item off an array from semi colon separated list using Velocity

Hi Jon,

Since your foreach variable is $session, could you try changing

#if( $lead.recommendedSession.contains($allSessions.sessionKey))


to

#if( $lead.recommendedSession.contains($session.sessionKey))


Hopefully it's as easy as that.

Jon_Wright
Level 4

Re: Picking first item off an array from semi colon separated list using Velocity

THANK YOU!!! I knew I was missing something simple

SanfordWhiteman
Level 10 - Community Moderator

Re: Picking first item off an array from semi colon separated list using Velocity

@Salytics is definitely right that you're ref'ing the wrong variable ($allSessions where you should have $session).

 

But that's not all. You do need to split the semicolon-delimited string as well, as you suggested. Using contains() on a delimited string is a really bad idea (think about "Session1" and "Session12").

#set( $allSessions = 
[{
  "sessionKey": "Session1",
  "sessionName":"Session 1 Name",
  "imageURL":"example.com/image1"
},
{
  "sessionKey": "Session2",
  "sessionName":"Session 2 Name",
  "imageURL":"example.com/image2"
}
])
#set( $leadSessionKeys = $lead.recommendedSession.split(";") )
#foreach( $session in $allSessions)
#if( $leadSessionKeys.contains($session.sessionKey))
#set( $firstRecommendedSession = $session )   
#break          
#end
#end
${firstRecommendedSession.sessionKey}
${firstRecommendedSession.sessionName}
${firstRecommendedSession.imageURL}

 

Jon_Wright
Level 4

Re: Picking first item off an array from semi colon separated list using Velocity

thanks @SanfordWhiteman - yep, I'm going to use split() - your Velocity advice is always appreciated.