SOLVED

Velocity script - List multiple values create different output for each single choice and if two or more choices are selected

Go to solution
Highlighted

Velocity script - List multiple values create different output for each single choice and if two or more choices are selected

Hi Sanford Whiteman

I'm trying to get this script to work. I need a condition for if two choices are selected you get a different output:

Here is my code:

#set( $outputBysubscriptionFunction = [

{

"pattern" : "(?i).*\b(144322355954892&&187699601952849)\b.*",

"greeting" : "Choices 1 and 2"

},

{

"pattern" : "(?i).*\b(187699601952849)\b.*",

"greeting" : "Choice 1"

},

{

"pattern" : "(?i).*\b(144322355954892)\b.*",

"greeting" : "Choice 2"

}

] )

#foreach( $outputSet in $outputBysubscriptionFunction )

#if( $lead.NL_Subscriptions__c.matches($outputSet.pattern) )

#set( $matchedOutputSet = $outputSet )

#break

#end

#end

<span>${matchedOutputSet.greeting}</span>

The result I'm getting is Choice 2 for when two choices are selected. Am I close? What am I missing?

Tags (1)
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Level 10 - Community Moderator

Re: Velocity script - List multiple values create different output for each single choice and if two or more choices are selected

Looks like a JSON-stringified array of numbers. JSON can be parsed into an object in Velocity, but I think I'll avoid that extra logic and treat this as a special case of a comma-delim'd string of numbers with some wrapper brackets. And ignore whitespace in case the original app throws some in later.

You don't want/need to use regexps here, just straight string equality.

#set( $NL_Subscriptions__cList = $lead.NL_Subscriptions__c.trim().replaceAll("^\[\s*|\s*\]$","").split("\s*,\s*") )

#set( $outputBysubscriptionFunction = [

{

"hasSubscriptions" : ["187699601952849", "144322355954892"],

"greeting" : "Choices 1 and 2"

},

{

"hasSubscriptions" : ["187699601952849"],

"greeting" : "Choice 1"

},

{

"hasSubscriptions" : ["187699601952849"],

"greeting" : "Choice 2"

}

] )

#foreach( $outputSet in $outputBysubscriptionFunction )

#if( $NL_Subscriptions__cList.containsAll($outputSet.hasSubscriptions) )

#set( $matchedOutputSet = $outputSet )

#break

#end

#end

<span>${matchedOutputSet.greeting}</span>

View solution in original post

4 REPLIES 4
Highlighted
Level 10 - Community Moderator

Re: Velocity script - List multiple values create different output for each single choice and if two or more choices are selected

You're not really that close. The regexp pattern is matching a single string, and the "&&" has no (special) meaning there.

What is the original value of NL_Subscriptions__c? A semicolon-delimited string representing one or more numeric strings?

Highlighted

Re: Velocity script - List multiple values create different output for each single choice and if two or more choices are selected

Thanks, for your response.

If two choices are selected the output is

[187699601952849,144322355954892]

Highlighted
Level 10 - Community Moderator

Re: Velocity script - List multiple values create different output for each single choice and if two or more choices are selected

Looks like a JSON-stringified array of numbers. JSON can be parsed into an object in Velocity, but I think I'll avoid that extra logic and treat this as a special case of a comma-delim'd string of numbers with some wrapper brackets. And ignore whitespace in case the original app throws some in later.

You don't want/need to use regexps here, just straight string equality.

#set( $NL_Subscriptions__cList = $lead.NL_Subscriptions__c.trim().replaceAll("^\[\s*|\s*\]$","").split("\s*,\s*") )

#set( $outputBysubscriptionFunction = [

{

"hasSubscriptions" : ["187699601952849", "144322355954892"],

"greeting" : "Choices 1 and 2"

},

{

"hasSubscriptions" : ["187699601952849"],

"greeting" : "Choice 1"

},

{

"hasSubscriptions" : ["187699601952849"],

"greeting" : "Choice 2"

}

] )

#foreach( $outputSet in $outputBysubscriptionFunction )

#if( $NL_Subscriptions__cList.containsAll($outputSet.hasSubscriptions) )

#set( $matchedOutputSet = $outputSet )

#break

#end

#end

<span>${matchedOutputSet.greeting}</span>

View solution in original post

Highlighted

Re: Velocity script - List multiple values create different output for each single choice and if two or more choices are selected

Thanks a lot! This works perfectly! You're the best!!!