Picking first item off an array from semi colon separated list using Velocity | Community
Skip to main content
Jon_Wright
New Participant
August 27, 2020
Solved

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

  • August 27, 2020
  • 2 replies
  • 3464 views

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?

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

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.

2 replies

SanfordWhiteman
New Participant
August 27, 2020

@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
New Participant
August 27, 2020

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

Salytics
SalyticsAccepted solution
New Participant
August 27, 2020

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
New Participant
August 27, 2020

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