SOLVED

Populating an email with custom object data that was triggered by a custom object data update.

Go to solution
Anonymous
Not applicable

Populating an email with custom object data that was triggered by a custom object data update.

Hello,

Marketo has a new feature that allows you to trigger an action based off of a custom object change.  Trigger Off Custom Object Changes - Marketo Docs - Product Docs

I need to populate a triggered email with the custom object data specific to the custom object change.  Normally, you can use $!{TriggerObject.CDOField} to populate triggered campaigns, but according to Marketo Support, velocity script does not work with custom object changes at this time.  Is there a workaround where I can pull specific fields from the CDO record that triggered the campaign?

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Populating an email with custom object data that was triggered by a custom object data update.

You don't need that backwards loop -- that's for reverse()ing an ArrayList based on natural index order.

To sort on updatedAt ​use SortTool:

#set( $sortedList = $sorter.sort($ObjectList,["updatedAt:desc"]) )

The filter you should do by declaring another ArrayList and populating it (we can't use actual filter predicates like in Java):

#set( $filteredList= [] )

#foreach( $itm in $sortedList)

#if( $itm.Field1 == "Published" )

#set( $tmp = $filteredList.add( $itm ) )

#end

#end

View solution in original post

9 REPLIES 9
SanfordWhiteman
Level 10 - Community Moderator

Re: Populating an email with custom object data that was triggered by a custom object data update.

If not surfaced as {{TriggerObject}}, you have to filter the ArrayList (of 10 custom objects) to get the most recently updated based on updatedAt or equivalent timestamp on the object itself.

Anonymous
Not applicable

Re: Populating an email with custom object data that was triggered by a custom object data update.

Hey Sanford,

How would I go about doing that?  Can you show some sample code?  I would need to sort the ArrayList and also filter based on certain CDO field criteria (i.e. Field1=Published).

I've seen this snippet you wrote for someone else:

  1. ## You can also use this var to hold the default event 
  2. #set( $lastInterestingEvent = {} ) 
  3. ## Guard against bad inputs (null or empty object list) 
  4. #if( $EventList && !$EventList.isEmpty() ) 
  5.     ## Loop backwards through ArrayList (standard foreach is fwd only) 
  6.     #foreach( $idx in [$math.sub($EventList.size(),1)..0] ) 
  7.         #set( $event = $EventList[$idx] ) 
  8.         #if( !$event.partnerURL.isEmpty() ) 
  9.             #set( $lastInterestingEvent = $event ) 
  10.             #break($foreach) 
  11.         #end 
  12.     #end 
  13. #end 
  14. Partner URL is: ${lastInterestingEvent.partnerURL} 

I found this very helpful, but would love to see how to edit it to sort by updatedAt and Field1=Published

Thanks!

SanfordWhiteman
Level 10 - Community Moderator

Re: Populating an email with custom object data that was triggered by a custom object data update.

You don't need that backwards loop -- that's for reverse()ing an ArrayList based on natural index order.

To sort on updatedAt ​use SortTool:

#set( $sortedList = $sorter.sort($ObjectList,["updatedAt:desc"]) )

The filter you should do by declaring another ArrayList and populating it (we can't use actual filter predicates like in Java):

#set( $filteredList= [] )

#foreach( $itm in $sortedList)

#if( $itm.Field1 == "Published" )

#set( $tmp = $filteredList.add( $itm ) )

#end

#end

Anonymous
Not applicable

Re: Populating an email with custom object data that was triggered by a custom object data update.

Hey Sanford, thank you for your help on this! Greatly appreciated!  One last thing, right now with code, when I use $tmp its returning a boolean, true/false value.  How would I populate with other fields (i.e. i want to show what's populated in Field3 when Field1 = Published AND Field2 = Google)?

SanfordWhiteman
Level 10 - Community Moderator

Re: Populating an email with custom object data that was triggered by a custom object data update.

Not sure what you want to "populate" here exactly? The $tmp is just a throwaway variable because Velocity doesn't allow a void (non-value-returning) function call, that is, you can't do

#set( $object.doSomething() )

you have to do

#set( $tmp = $object.doSomething() )

even if you have no use for $tmp.

So all we're doing is calling ArrayList.add().

Anonymous
Not applicable

Re: Populating an email with custom object data that was triggered by a custom object data update.

Sorry for not explaining clear enough. 

So i have a CDO for a one to many relationship with multiple fields per record: productName, productID, status, site

I have a triggered campaign to launch when a the status field in the CDO changes.  I want it to trigger an email and have it be populated with the specific productName of the record in the CDO that updated, as long as it fits certain criteria (status = Published AND site=Google).

I'm new to Velocity and would love a good base to work off of.

Thanks

SanfordWhiteman
Level 10 - Community Moderator

Re: Populating an email with custom object data that was triggered by a custom object data update.

In this way Velocity is like most other C-variant languages. When you filter down to the new array, include conditions separated by || or &&.

#if( $itm.Status == "Published" && $itm.site == "Google" )

Anonymous
Not applicable

Re: Populating an email with custom object data that was triggered by a custom object data update.

I was able to get it work.  Thank you again for all your help.  You are definitely one of the crucial brains to this community!  I'm sure I'll be asking more questions that you'll be able to answer very soon!

SanfordWhiteman
Level 10 - Community Moderator

Re: Populating an email with custom object data that was triggered by a custom object data update.

Good to hear!