SOLVED

Validation error approving with Email Approve when using the Velocity Code

Go to solution
Level 3

Validation error approving with Email Approve when using the Velocity Code

I am facing the issue when approving email below is the error which Marketo throws Validation Error approving

 

 

An error occurred when procesing the email Rendered_Email_Velocity_Error_Area_?!

Error invoking method 'get(java.lang.Integer)' in java.util.ArrayList near

 

  below is my VTL code which I tried to implement in email and when I preview the sample email it work perfect but when approving the email it throws above error

 

 

 

#set( $default_Name_Recipe_1 = "#" )
#set( $arrCounter = 0 )
#set ($index = 0)
#set( $prodRecoArr = [""] )
#set( $recoURL = [""] )
#set($one = 1)
#if( $CO_cList.isEmpty() )
#set( $defaultNameRec1 = $default_Name_Recipe_1 )
#else
#foreach( $CO_c in $sorter.sort($CO_cList, "recommendationDateStart:desc") )
#if( !$display.alt($CO_c["recommendationURL"],"").isEmpty()&& ($CO_c["recommendationName"]=="Recipe"))
#set($prodRecoArr[${arrCounter}] = $CO_c["recommendationURL"])
#set( $arrCounter = $arrCounter + 1)
#break
#end
#end
#end
#if ($prodRecoArr.size() >= 1 && ($CO_c["recommendationName"]=="Recipe") )
#set($index = $arrCounter - 1)
#set( $recoURL = ${prodRecoArr[${index}].split("//")})
<a href="https://${recoURL[$one]}">VTL test</a>
#else 
${defaultNameRec1}
#end

 

 In the above VTL, I am displaying data from CO table for product URL and using in email. Can anyone help on this issue? 

1 ACCEPTED SOLUTION

Accepted Solutions
Level 10 - Community Moderator

Re: Validation error approving with Email Approve when using the Velocity Code

The direct cause of the error is here:

 

<a href="https://${recoURL[$one]}">Recipe Link</a>

 

 

You can't dereference item 1 of an array that has only one item (item 0). And there's only one item when you use the default value, the list with one item[""].

 

But really the entire code block is deeply flawed. The magic constant $one, for example, is overengineered: just use Integer 1. I see no reason to use the counter at all. And splitting is unnecessary if the business goal is to strip out the protocol: just, well, replace the protocol with an empty string.

 

Should be more like this:

 

#set( $recoURL_default = "#" )
#if( !$CO_cList.isEmpty() )
#foreach( $CO_c in $sorter.sort($CO_cList, "recommendationDateStart:desc") )
#if( !$display.alt($CO_c["recommendationURL"],"").isEmpty() && $CO_c["recommendationName"].equals("Recipe") )
#set( $recoURL = $CO_c["recommendationURL"].replaceFirst("^https?://","") )
#break
#end
#end
#end
#if( !$display.alt($recoURL,"").isEmpty() )
<a href="https://${recoURL}">Recipe Link</a>
#else
<a href="${recoURL_default}">Recipe Link</a>
#end

 

View solution in original post

5 REPLIES 5
Level 10 - Community Moderator

Re: Validation error approving with Email Approve when using the Velocity Code

The error means you're trying to dereference a list index that doesn't exist, that is, $someList[0] when the list is empty, or $someList[1] when it has only 1 item, etc.

 

Your code is rather opaque so unless you document it further or describe the inputs and outputs more deeply, couldn't tell you what's leading you to that fatal error.

Level 3

Re: Validation error approving with Email Approve when using the Velocity Code

Somehow, I missed one condition in VTL which I have added now.

In the given VTL,CO table CO_c have the following value recommendationDateStartrecommendationURL and recommendationName and through VTL code want to generate tracking URL when recommendationName has value as Recipe . And in above VTL following things I am trying to do

1. If CO_c is empty for a Lead then display default value

2. Else, based on recommendationName generate the traceable URL

3. As, I have used foreach loop condition I have declared array variable  i.e. prodRecoArr so that can assign one URL at one time while using arrCounter variable in this too.
4. At the end of  loop condition and first, second IF condition. Evaluating  the recommendationURL details and splitting the URL to use correctly in anchor tag <a> </a>.  Based on third, IF condition.

Level 10 - Community Moderator

Re: Validation error approving with Email Approve when using the Velocity Code

Not really clear. There's a lot in the code that isn't in your summary.

 

Why are you sorting the list?

 

If you only end up with a single object, why aren't you exiting the loop when you find that object?

 

Why are you referring to the loop variable outside of the loop? (This is possible in Velocity because it's in the global scope, but still shouldn't be done.)

 

Why are you incrementing

 your own loop index instead of using the built-in index?

 

Level 3

Re: Validation error approving with Email Approve when using the Velocity Code

Firstly, I have modified the code as I see scope of global variable is not needed. 

#set( $default_Name_Recipe_1 = "#" )
#set( $arrCounter = 0 )
#set( $prodRecoArr = [""] )
#set( $recoURL = [""] )
#set($one = 1)
#if( $CO_cList.isEmpty() )
#set( $defaultNameRec1 = $default_Name_Recipe_1 )
#else
#foreach( $CO_c in $sorter.sort($CO_cList, "recommendationDateStart:desc") )
#if( !$display.alt($CO_c["recommendationURL"],"").isEmpty()&& ($CO_c["recommendationName"]=="Recipe"))
#set($prodRecoArr[${arrCounter}] = $CO_c["recommendationURL"])
#set( $recoURL = ${prodRecoArr[${arrCounter}].split("//")})
#break
#end
#end
#end
<a href="https://${recoURL[$one]}">Recipe Link</a>

List sorting in above VTL code is due to CO table data get updated weekly so sorting the data to get the most recent data only based on date field.

No more loop iteration after getting the single objects in modified VTL code. Also, not need to print the default value.

 

Index has been removed as this was used in global variable reference.

 

Finally, business use case is to generate the traceable dynamic link based on recent data added as well as recommendationName which vary irregularly. Still, I am facing same issue to approve the email.

Level 10 - Community Moderator

Re: Validation error approving with Email Approve when using the Velocity Code

The direct cause of the error is here:

 

<a href="https://${recoURL[$one]}">Recipe Link</a>

 

 

You can't dereference item 1 of an array that has only one item (item 0). And there's only one item when you use the default value, the list with one item[""].

 

But really the entire code block is deeply flawed. The magic constant $one, for example, is overengineered: just use Integer 1. I see no reason to use the counter at all. And splitting is unnecessary if the business goal is to strip out the protocol: just, well, replace the protocol with an empty string.

 

Should be more like this:

 

#set( $recoURL_default = "#" )
#if( !$CO_cList.isEmpty() )
#foreach( $CO_c in $sorter.sort($CO_cList, "recommendationDateStart:desc") )
#if( !$display.alt($CO_c["recommendationURL"],"").isEmpty() && $CO_c["recommendationName"].equals("Recipe") )
#set( $recoURL = $CO_c["recommendationURL"].replaceFirst("^https?://","") )
#break
#end
#end
#end
#if( !$display.alt($recoURL,"").isEmpty() )
<a href="https://${recoURL}">Recipe Link</a>
#else
<a href="${recoURL_default}">Recipe Link</a>
#end

 

View solution in original post