SOLVED

Re: Velocity email script split data based on delimiter "|"

Go to solution
Jay_sandbox_Sin
Level 2

Velocity email script split data based on delimiter "|"

In CO table, one of the field has product value in below format

product =  productname | productimage | productcode

My pain point is to split the data but I am getting below issue

Cannot get email content- <div>An error occurred when procesing the email Body! </div> <p>Encountered "]" near</p> <div><pre >&lt;p&gt;&lt;span style=&quot;font-family: Verdana;&quot;&gt;#set( $productArr = [&quot;1&quot;,&quot;2&quot;,&quot;3&quot;] )</pre><pre >#if( !$display.alt($CO_c[&quot;product&quot;],&quot;&quot;).isEmpty())</pre><pre class="x-form-item-label">#set( $productArr[] = $CO_c[&quot;product&quot;].split(&quot;|&quot;) )</pre><pre >$productArr[1]</pre><pre >$productArr[2]</pre></div>

 

I have tried below sample VTL code.

##Using split("|") function to seprate the data sets

#set( $productArr = ["1","2","3"] )
#if( !$display.alt($CO_c["product"],"").isEmpty())
#set( $productArr[] = $CO_c["product"].split("|") )
$productArr[1]
$productArr[2]
$productArr[3]
Tags (1)
1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity email script split data based on delimiter "|"

I'm referring to the Velocity name of the object list and key.

 

Not the API name, that must be considered completely different (even if it is closely related).

 

You only know the Velocity name by dragging it onto the Velocity Script Editor canvas.

View solution in original post

9 REPLIES 9
Abhinav_saini
Level 3

Re: Velocity email script split data based on delimiter "|"


Hi Jay,

 

I think there are some syntax errors.  Just refer to earlier post, you'll get your answer.

https://nation.marketo.com/t5/Product-Discussions/Velocity-Splitting-a-string-into-an-array/m-p/1446...

 

xx

 

Abhinav Saini
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity email script split data based on delimiter "|"

There's a syntax error. You're missing an #end.

 

But even when you fix that, it still won't do what you expect, because the argument you're passing to split() is a regex pattern, not a string. As such, the pipe needs to be escaped as

[|]

 

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity email script split data based on delimiter "|"

And also

  • you're redeclaring $productArr quite confusingly
  • there's another fatal syntax error: $productArr[] is not valid Velocity, it would just be $productArr

Pretty lost as to what you're trying to accomplish here. Can you please describe the business goal without code?

Jay_sandbox_Sin
Level 2

Re: Velocity email script split data based on delimiter "|"

In custom objects, one object field data is being pushed with this combination "productname | productimage | promocode

Business goal is to display product name,  image and promo code details in the email. So, splitting the data based on delimiter (|) and used the VTL token in email to get the output.

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity email script split data based on delimiter "|"

There's a space character, too, on either side of the pipes in your example.

 

I'm going to ignore the whitespace, but please be precise when giving example data.

 

All you need here is

#set( $productParts = $CO_c.product.split("[|]") )

 

Note split() returns an Array, not a ArrayList, though that shouldn't make a difference in your case.

Jay_sandbox_Sin
Level 2

Re: Velocity email script split data based on delimiter "|"

Hi @SanfordWhiteman 

single object product value is a|b|c added for testing. 

#if(!$display.alt($CO_c["product"],"").isEmpty())
#set( $productParts = $CO_c.product.split("[|]") )
#end
$productParts[0]
$productParts[1]
$productParts[2]

Although, I am not getting any error but display value is not as desired. It print as a string $productParts[1]  and so on.

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity email script split data based on delimiter "|"

That code works fine if indeed the key named

product

 

on a map named

$CO_c

 

is set to the string

1|2|3

 

If you're using the wrong key name, sending a sample, or any other case where $CO__c doesn't have a key named product, then you would see the literal code output instead (that's what Velocity outputs by default when values are null).

Jay_sandbox_Sin
Level 2

Re: Velocity email script split data based on delimiter "|"

Hi @SanfordWhiteman 

 

CO and keyname all are correct. please refer below screenshot.

Jay_sandbox_Sin_0-1606895502711.png

Although, In Marketo last we saw issue deleting the CO even after CO was not used anywhere in Marketo. After that Marketo support team fixed that issue. But, now in this case I am not sure if I should reach out to support team because they do not support coding part.

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity email script split data based on delimiter "|"

I'm referring to the Velocity name of the object list and key.

 

Not the API name, that must be considered completely different (even if it is closely related).

 

You only know the Velocity name by dragging it onto the Velocity Script Editor canvas.