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?
Solved! Go to Solution.
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>
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?
Thanks, for your response.
If two choices are selected the output is
[187699601952849,144322355954892]
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>
Thanks a lot! This works perfectly! You're the best!!!