SOLVED

Store objects in custom fields

Go to solution
WolframLotz
Level 4

Store objects in custom fields

Hi there, 

 

We are using different online calculator which post their data as for post. In this form post all data are seperate data field. 

The disadvantage is that all this filed will be overwritten in case of the next form post. 

 

We like to store this data as object within one custom field. From what I read the best way would be to use a custom text area field. As custom ojects data can't be stored via smart campaign flows we like to use a custom field. 

 

There are several posts on this topic but none of them worked for me.

From what I found out there are ways like the following:

https://nation.marketo.com/t5/product-discussions/tokenising-json-field-in-marketo/td-p/27919 

Info from @SanfordWhiteman 


You store data as JSON 

{ "id" : 123, "booking_id" : "ABC","tax" : 1.875 }

 

You transform this data using "set" and "evalutate"

#set( $jsonProduct = '#set( $jsonProduct = ' + $Bookings2_cList[0].jsonProduct + ' )' )

#evaluate( $jsonProduct )

 

After this that data should be transformed into an velocity map. 

{id=123, booking_id=ABC, tax=1.875}

 

I tried to get the data using a request like $object.id. But in my cas it didn't work. 



Do you know what I do wrong?
How shoud I store data in my marketo custom data fields? (not in a custom object)

How can I add this data to an emaily velocity script? 

How can I access a specific information via Key?

What do I have to consider for the values (does and don'ts)?

Thanks a lot. 

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Store objects in custom fields


You store data as JSON 
{ "id" : 123, "booking_id" : "ABC","tax" : 1.875 }

 

You transform this data using "set" and "evalutate"

#set( $jsonProduct = '#set( $jsonProduct = ' + $Bookings2_cList[0].jsonProduct + ' )' )
#evaluate( $jsonProduct )

The pattern is:

#set( $hydrate = "${esc.h}set( ${esc.d}hydratedObject = " + $jsonString + " )" )
#evaluate( $hydrate )

 

Then $hydratedObject is an ArrayList or LinkedHashMap with nested properties. All the usual Java (6) methods are usable.

 

The important guidelines for the JSON are:

  • you can’t use null (it’s not a meaningful keyword in VTL)
  • you must use long Unicode escapes like \u000A, not short “macro escapes” like \n

 

 

View solution in original post

6 REPLIES 6
SanfordWhiteman
Level 10 - Community Moderator

Re: Store objects in custom fields


You store data as JSON 
{ "id" : 123, "booking_id" : "ABC","tax" : 1.875 }

 

You transform this data using "set" and "evalutate"

#set( $jsonProduct = '#set( $jsonProduct = ' + $Bookings2_cList[0].jsonProduct + ' )' )
#evaluate( $jsonProduct )

The pattern is:

#set( $hydrate = "${esc.h}set( ${esc.d}hydratedObject = " + $jsonString + " )" )
#evaluate( $hydrate )

 

Then $hydratedObject is an ArrayList or LinkedHashMap with nested properties. All the usual Java (6) methods are usable.

 

The important guidelines for the JSON are:

  • you can’t use null (it’s not a meaningful keyword in VTL)
  • you must use long Unicode escapes like \u000A, not short “macro escapes” like \n

 

 

WolframLotz
Level 4

Re: Store objects in custom fields

Hi @SanfordWhiteman
thank you. Unfortunatley it did not work for me. I tried it the following:


Creat a custom field of type text area.  

WolframLotz_0-1669202039537.png

 

That field could be requested in velocity using the following token

 

$lead.mktoTestTextArea

 

 

I try to use your code while the $jsonString is exchanged against my custom field token. 

 

#set( $hydrate = "${esc.h}set( ${esc.d}hydratedObject = " + $lead.mktoTestTextArea + " )" )
#evaluate( $hydrate )

 


If I try to use this email script token in my mail I get the following error message:
annot get email content- 

 

<div>An error occurred when procesing the email Body! </div> <p>Encountered ")" near</p> <div><pre ></pre></div>

 

 

 

What am I doing wrong?

WolframLotz
Level 4

Re: Store objects in custom fields

Hi @SanfordWhiteman 
I have no idea what changed. But now I was able to use your code and I was able to access the data using "get()" methode. 
Great! Thanks!

I'm just wondering. Is Marketo able to "work" on that objects properly?

 

I was able to access the data. But if I try to use a for loop nothing happens.

$RwmOffers[0] // gives me the JSON of the corresponding entry
$RwmOffers[0].get("option1") // gives me the correct value 
$RwmOffers[0].get("option1").equals($lead.valueToFind) // gives me true 
$RwmOffers.size() // shows the correct size

//nothing happens here
#foreach( $item in $RwmOffers )
$item.get('option1')
#end

 
Strange thing as well - I wasn't able to stop Marketo accessing an empty array. 
During my research I found as well your blog post https://blog.teknkl.com/a-little-velocity-math-trick-and-a-note-on-empty-lists/ 
But what ever I tried my if-case was executed the same way like an array with elements. 

$ClientReferrals.size() // output:0
$ClientReferrals.size().compareTo(0) //output:0
$ClientReferrals.size().equals(0) //ouput:true

// based on the 'true'
#if($ClientReferrals.size().equals(0))
#set($result = 'It is empty')
#else
#set($result = 'There is content')
#end
$result //ouput:'There is content'

 Do you have any ideas where my mistake is?

SanfordWhiteman
Level 10 - Community Moderator

Re: Store objects in custom fields


I'm just wondering. Is Marketo able to "work" on that objects properly?

Of course. But all the standard Java rules apply.

 

You can’t iterate over a HashMap (if that’s what your outer object is). You can iterate over its entries or its keys or its values, but you can’t just #foreach it.

WolframLotz
Level 4

Re: Store objects in custom fields

@SanfordWhiteman 

ah sorry, should have mentioned this. $RwmOffer is an array where the single entries are HashMaps. Therefor I tried to use the #foreach loop on an object compareable to what you did hear with Clients_Referrals__chttps://blog.teknkl.com/marketo-json-fields-in-velocity/

I guess a #foreach loop should work on an array. Or is this wrong?

It's strange. If I declare an array directly in the script token everything works fine. In that case I can use the foreach loop.

//example
#set($array = [{"key":"value"}{"key2":"value2"}{"key3":"value3"}])


But if I try to get data from a data field an (a) use .split() or (b) use the conversion you mentioned. Than I can't use the foreach loop and several action on that array seem to fail. 

SanfordWhiteman
Level 10 - Community Moderator

Re: Store objects in custom fields

I guess a #foreach loop should work on an array. Or is this wrong?


On an Array or ArrayList, yes.


But if I try to get data from a data field an (a) use .split() ... then I can't use the foreach loop and several action on that array seem to fail.

String.split() creates an Array. Velocity #foreach ensures any collection object, including an Array, has a real iterator so it can be iterated. I iterate over the results of String.split() constantly — probably with thousands of different {{my.tokens}} in the wild doing this — and there’s never been a problem.