SOLVED

Re: Email scripting with custom object - show whole list, but only particular fields

Go to solution
Phillip_Wild
Level 10

Email scripting with custom object - show whole list, but only particular fields

Hi team

I'm trying to output a custom object, but only a particular field. So let's say I have a custom object named CustomObject with the fields Name and Description. I can use:

${CustomObject}

To output all fields. Great. I can also use:

${CustomObject.get(0).Name}

To output the first record of that custom object, and the Name field.

But....I can't for the life of me work out how to output the Name field for every record in that custom object. I've tried things like:

${CustomObject.Name}

${CustomObject.get.Name}

${CustomObject.get().Name}

With no luck. What am I missing here? I'm sure this can't be that hard....

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Email scripting with custom object - show whole list, but only particular fields

P.S. The alternative method Nicho is driving at is this:

#set( $allNames = [] )

#foreach( $object in $CustomObjectList )

#set( $tmp = $allNames.add($object.Name) )

#end

Naturally, I like my method more as it's shorter + lets you take advantage of Velocity's underlying Collections support.

Also, for those unfamiliar, Unicode/ASCII character x001e (31) is the Record Separator (RS) character. This is the only character guaranteed per formal standard to not be found within a value, but to only be a separator between values.

That is, we often treat commas and semicolons as separators, but that only works if you know ahead of time that your values will never contain commas and semis, or if you have an additional layer to act as an escape mechanism (double quotes)... which then in turn needs to be escaped... covering all the bases can take a lot of code.

In contrast, if you just use RS between values you're always safe, no need for other escaping. To put it more simply, join(RS).split(RS) always works even if you don't know the data.

View solution in original post

12 REPLIES 12
SanfordWhiteman
Level 10 - Community Moderator

Re: Email scripting with custom object - show whole list, but only particular fields

## @param1 list of objects

## @param2 delimiter between all but last 2 objects

## @param3 delimiter between last 2 objects

## @param4 extract only this property from each object

$display.list(

  $CustomObjectList,

  "<br>",

  "<br>",

  "Name"

)

Phillip_Wild
Level 10

Re: Email scripting with custom object - show whole list, but only particular fields

Hi Sanford

Thanks! However, I'm afraid I might have to modify the question a little. I'm now looking to pass only those items from the list into a new list. So in my example from before, I want to end up with a list which looks like this:

$names = [$name1, $name2, $name3....$namen]

Since your code will display the items, I think I need something else. I tried:

#foreach( $name in $CustomObject)

#set ($dummy = $names.add(${CustomObject.get(0).dossierCode))

#end

I can't even get this to work....although I know it's going to add the item at index 0 over and over again (I guess foreach.index or velocity.count is appropriate here?)

SanfordWhiteman
Level 10 - Community Moderator

Re: Email scripting with custom object - show whole list, but only particular fields

#set( $allNames = $display.list($CustomObjectList,"\u001e","\u001e","Name").split("\u001e") )

$display.list doesn't actually force output, it just creates a string, which you can then split.

Also, can you use the syntax highlighter please? I tell everyone else, shouldn't let you off the hook.

pastedImage_2.png

SanfordWhiteman
Level 10 - Community Moderator

Re: Email scripting with custom object - show whole list, but only particular fields

P.S. The alternative method Nicho is driving at is this:

#set( $allNames = [] )

#foreach( $object in $CustomObjectList )

#set( $tmp = $allNames.add($object.Name) )

#end

Naturally, I like my method more as it's shorter + lets you take advantage of Velocity's underlying Collections support.

Also, for those unfamiliar, Unicode/ASCII character x001e (31) is the Record Separator (RS) character. This is the only character guaranteed per formal standard to not be found within a value, but to only be a separator between values.

That is, we often treat commas and semicolons as separators, but that only works if you know ahead of time that your values will never contain commas and semis, or if you have an additional layer to act as an escape mechanism (double quotes)... which then in turn needs to be escaped... covering all the bases can take a lot of code.

In contrast, if you just use RS between values you're always safe, no need for other escaping. To put it more simply, join(RS).split(RS) always works even if you don't know the data.

Phillip_Wild
Level 10

Re: Email scripting with custom object - show whole list, but only particular fields

Brilliant! Thanks everyone.

I worked out the key problem is that I actually wasn't creating the null array before I looped. Such an easy fix!!!!

Thanks. And I'll try to remember the code trick Sanford, sorry!

Chase_Carson
Level 1

Re: Email scripting with custom object - show whole list, but only particular fields

I'm using this code and it is outputting correctly, but wondering how do I remove the [] around the data?

#set ($allNames = [])
#foreach( $object in $accountsOwned_cList )
#set( $tmp = $allNames.add($object.product) )
#end
${allNames}

 

Outputs [Product 1, Product 2]

Probably a simple answer but wondering how I get it to output without the [] surrounding it

SanfordWhiteman
Level 10 - Community Moderator

Re: Email scripting with custom object - show whole list, but only particular fields

A list's string representation always has brackets and commas. How do you want the output to look?

Chase_Carson
Level 1

Re: Email scripting with custom object - show whole list, but only particular fields

Hi Sandford,

 

Thanks for responding!

I just wanted it to output: Product 1, Product 2 without the brackets. 

I'm new to velocity and am able to do with this with the #set ($Variable = ''") and then printing the variable at the end ${Variable} on person level tokens but am new to using it with Custom Object fields and for each loops.

SanfordWhiteman
Level 10 - Community Moderator

Re: Email scripting with custom object - show whole list, but only particular fields

${display.list($allNames,", ")}