Hello community. I am new to velocity scripting and working on printing split values from a multi-value field into a table. I have a few different approaches to the script but have been unable to get the results we are seeking. Would appreciate any help from the community.
My multi-field value (Event_Sessions__c) is storing session preferences from a form field as
10am session1; 11am session2; 12PM Session3;
This is my first iteration of the script but I keep getting errors:
#if( $lead.Event_Sessions__c.isEmpty() )
#set( $lead.Event_Sessions__c = '[]' )
#end
#set( $EventSessions = $lead.Event_Sessions__c )
#set( $SplitEvents = $EventSessions.split(";") )
<table>
<tbody>
<tr>
<td>$SplitEvents[0]</td>
</tr>
<tr>
<td>$SplitEvents[1]</td>
</tr>
<tr>
<td>$SplitEvents[2]</td>
</tr>
<tr>
<td>$SplitEvents[3]</td>
</tr>
<tr>
<td>$SplitEvents[4]</td>
</tr>
<tr>
<td>$SplitEvents[5]</td>
</tr>
<tr>
<td>$SplitEvents[6]</td>
</tr>
</tbody>
</table>
any tips would be appreciated.
Solved! Go to Solution.
Remember that “I keep getting errors” alone isn’t useful. Without knowing what the errors are, we have to run your code to even start seeing what you’re seeing.
I assume the errors you’re seeing from your 1st version are of the form Error invoking method 'get(java.lang.Integer)'
. Such errors are expected when you try to access nonexistent array indices. There’s no guarantee at all that there will be anything beyond $SplitEvents[0]
since you don’t know the number of sub-items in the string.
Note it is guaranteed that there will be at least $SplitEvents[0]
even if $lead.Event_Sessions__c
is an empty string. Splitting an empty string always yields an array with at least one item!
But there’s no reason to check isEmpty()
and then override $lead.Event_Sessions__c
with the string '[]'
. That’s just going to leave you with a one-item array with the literal string '[]'
at index 0. Doesn’t really solve anything.
To output one row and column for each session, simply check isEmpty()
and then loop over the sessions:
#if( !$lead.Event_Sessions__c.isEmpty() )
<table>
#set( $AllSessions = $lead.Event_Sessions__c.split("\s*;\s*") )
#foreach( $Session in $AllSessions )
<tr>
<td>$Session</td>
</tr>
#end
</table>
#end
Note I also modified the split()
because you weren’t taking into account the extra whitespace in your example.
This is another approach that does not give me any errors but I am not getting the output:
#if( $lead.Event_Sessions__c.isEmpty() )
#set( $lead.Event_Sessions__c = '[]' )
#end
#set( $EventSessions = $lead.Event_Sessions__c )
#set( $SplitEvents = $EventSessions.split(";") )
#foreach($session in $SplitEvents)
#if( $session[0] != 0)
#set( $sessionOne = $session[0] )
#set( $sessionTwo = $session[1] )
#end
#end
<table>
<tbody>
<tr>
<td>$sessionOne</td>
</tr>
<tr>
<td>$sessionTwo</td>
</tr>
</tbody>
</table>
Your 2nd version also has some very strange code. Not sure what the intent is behind the condition
#if( $session[0] != 0)
but it’s never going to be true and isn’t going to solve any problems.
Remember that “I keep getting errors” alone isn’t useful. Without knowing what the errors are, we have to run your code to even start seeing what you’re seeing.
I assume the errors you’re seeing from your 1st version are of the form Error invoking method 'get(java.lang.Integer)'
. Such errors are expected when you try to access nonexistent array indices. There’s no guarantee at all that there will be anything beyond $SplitEvents[0]
since you don’t know the number of sub-items in the string.
Note it is guaranteed that there will be at least $SplitEvents[0]
even if $lead.Event_Sessions__c
is an empty string. Splitting an empty string always yields an array with at least one item!
But there’s no reason to check isEmpty()
and then override $lead.Event_Sessions__c
with the string '[]'
. That’s just going to leave you with a one-item array with the literal string '[]'
at index 0. Doesn’t really solve anything.
To output one row and column for each session, simply check isEmpty()
and then loop over the sessions:
#if( !$lead.Event_Sessions__c.isEmpty() )
<table>
#set( $AllSessions = $lead.Event_Sessions__c.split("\s*;\s*") )
#foreach( $Session in $AllSessions )
<tr>
<td>$Session</td>
</tr>
#end
</table>
#end
Note I also modified the split()
because you weren’t taking into account the extra whitespace in your example.
Thank you for the explanation. I have a working script now. I will keep improving my programming skills and hope to someday give back to the community.
Sounds good!