Re: Cannot apply string's functions to an output obtained from LEAD/CUSTOM objects

Anonymous
Not applicable

Cannot apply string's functions to an output obtained from LEAD/CUSTOM objects

Hi,

I'm trying to split a string into parts using 'colon' as delimiter. Given $number = "10:20:30", I need to loop through the whole string and do something to each number in it, eg. output "10" then output "20" and then "30" and end the loop. My code reads:

#set ($numberSet = "10:20:30")

#foreach ($number in $numberSet.split("[:]"))

$number

#end

------------------

OUTPUT:

10 20 30

------------------

which works fine. However, if I try to obtain the value from LEAD/CUSTOM objects (which has the exact same value), the code will read:

#set ($numberSet = "{{lead.numberSet:default=edit me}}")

#foreach ($number in $numberSet.split("[:]"))

$number

#end

------------------

OUTPUT:

10:20:30

------------------

From above, the split() function will not work. It isn't just split() function but any other string's function won't be working at all (replace, substring). I tried getClass() and its output was "class java.lang.String". Despite the output showing it is string type, no string's functions I can apply with it.

So essentially, I need to know how to deal with the value from LEAD/CUSTOM objects. Are they being treated as JSON? If so, how to get the context of the LEAD's variable (10:12:14) and store it to a variable and use string's functions with it.

Thank you.

9 REPLIES 9
SanfordWhiteman
Level 10 - Community Moderator

Re: Cannot apply string's functions to an output obtained from LEAD/CUSTOM objects

[a] No reason to use a character class. You can use a literal :

[b] You don't use the handlebars syntax:

{{lead.numberSet:default=edit me}}

in Velocity. You use Velocity reference notation:

$lead.numberSet

Nicholas_Manojl
Level 9

Re: Cannot apply string's functions to an output obtained from LEAD/CUSTOM objects

Is it not also a syntax error?

shouldn't it be

#foreach ($number in $numberSet.split(":", x))

(where x is the is the amount of strings you want to split into, starting at 1)

SanfordWhiteman
Level 10 - Community Moderator

Re: Cannot apply string's functions to an output obtained from LEAD/CUSTOM objects

(where x is the is the amount of strings you want to split into, starting at 1)

Nah, split() is overloaded, the constructor with just a regex is fine (no limit).

Nicholas_Manojl
Level 9

Re: Cannot apply string's functions to an output obtained from LEAD/CUSTOM objects

Yeh, that bit about the regex went straight past me but I see that now.

Anonymous
Not applicable

Re: Cannot apply string's functions to an output obtained from LEAD/CUSTOM objects

Thank you for your answer, Sanford.

[a]

Could you please explain more regarding [a] ? All I did was checking the variable type for the data obtained from the LEAD and it is showing as string yet I cannot split them as it should. Will using literal class help solving the issue and if so how do I set it up?

[b]

And for [b], as I am using TOKENs, the syntax will be handlebars. Is there a way I can directly access LEAD/CUSTOM objects without using TOKEN's syntax?

My current LEAD token reads:

{{lead.numberSet:default=edit me}}

I tried

$lead.numberSet

didn't work.

The LEAD belongs to PEOPLE >> INFO in Marketo's DATABASE section.

pastedImage_7.png

Thank you.

SanfordWhiteman
Level 10 - Community Moderator

Re: Cannot apply string's functions to an output obtained from LEAD/CUSTOM objects

[a] Could you please explain more regarding [a] ? All I did was checking the variable type for the data obtained from the LEAD and it is showing as string yet I cannot split them as it should. Will using literal class help solving the issue and if so how do I set it up?

This regex

[:]

is no different from the single character

:

Nothing is gained by making it a set with a single character.

[b] And for [b], as I am using TOKENs, the syntax will be handlebars.

Incorrect.

Within Velocity, you never use handlebars syntax.

When you select your field from the tree on the right-hand-side of Script Editor and drag it onto the script canvas (selecting it is critical), you will see its Velocity reference name, which is the friendly name stripped of spaces and special characters. For example, the field Last Form Fillout could be VTL $lead.LastFormFillout.

Anonymous
Not applicable

Re: Cannot apply string's functions to an output obtained from LEAD/CUSTOM objects

Within Velocity, you never use handlebars syntax.

What about the LEADs field which we can use without assigning them as CUSTOMs ? When I use them via TOKEN's function:

pastedImage_2.png

These LEAD tokens need no custom assignments (selecting them from tree on right hand side), hence they will never show its 'not-handle-bars' version, eg. I don't know how to transfer from

{{lead.uDecide listOfOffers}}

to

$lead.uDecidelistOfOffers

Or is it a must to do token assignment each time?

FYI, if I assign

$offers = {{lead.uDecide listOfOffers}}

and print it on the email, it will perfectly shows output as:

70:90:100

but the case is I cannot apply split() with it.

Please help clarify as you see fit. I am a complete newbie to Velocity. Thank you.

SanfordWhiteman
Level 10 - Community Moderator

Re: Cannot apply string's functions to an output obtained from LEAD/CUSTOM objects

I can't help you if you keep saying "I don't need to..." about things I've told you 2x are necessary. Follow my directions: select the field from the tree and drag it into the script to determine its VTL name.

You can read more about Velocity w/Marketo at http://blog.teknkl.com/tag/velocity and by searching my posts here.

Anonymous
Not applicable

Re: Cannot apply string's functions to an output obtained from LEAD/CUSTOM objects

I think I get it now. I'll try this approach. Thank you !