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?
Solved! Go to Solution.
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.
@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}
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.
THANK YOU!!! I knew I was missing something simple
@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}
thanks @SanfordWhiteman - yep, I'm going to use split() - your Velocity advice is always appreciated.