Email Scripting - Filtering Unique Records

Amey_Shivapurka
Level 2

Email Scripting - Filtering Unique Records

Does anybody know a way to filter/select only unique values of a certain field from a foreach statement via email scripting? I

 

We have different coverage type in addition to the Registration. The email has a table that includes the customer's Serial Number and Product Coverage combined into one row. The problem is that they are all part of the same object and the main registration and coverage are tied together via the same serial number, so this is creating more line items than we need.

 

So I'm trying to look for a way to just filter my list to only show unique Serial Numbers per line item.  

9 REPLIES 9
Jo_Pitts1
Level 10 - Community Advisor

Re: Email Scripting - Filtering Unique Records

@Amey_Shivapurka ,

can you show us:

  • What you mean around the data (some screenshots or examples)
  • What you want your output to be
  • What you've attempted so far

I THINK I know what you're looking for, but I'm not 100% certain. 

 

Cheers

Jo

Jo_Pitts1
Level 10 - Community Advisor

Re: Email Scripting - Filtering Unique Records

@Amey_Shivapurka ,

you also might want to review the solution in this thread (posted by you).

https://nation.marketo.com/t5/product-discussions/capture-specific-type-from-co-with-velocity-script...

 

That might get you close.

 

Regards

Jo 

Amey_Shivapurka
Level 2

Re: Email Scripting - Filtering Unique Records

Hi @Jo_Pitts1 , yeah I used that solution as my base and its got me close. I was able to get a list of all the asset type. A sample output is below for a customer is below. The first 2 rows is just what I want and then not show the red text rows. Because the serial number column and product protection share the same serialNumber. I would like to just only how unique value of serial numbers. I have another solution in place and forgot to save the code snippet for the below, but its inline basically with the code snippet from my previous post you listed above. 

 

A sample output looks like this:

Serial Number [type__c][serialNumber] Limited Warranty [usageDate] Product Protection [type_-c][usageDate]
ABCDEFG 10/21/21 - 12/21/24 Gold: 10/21/24-10/21/26
ZYXWVU 10/21/21 - 12/21/24 Plat: 10/21/24-10/21/26
ABCDEFG 10/21/21 - 12/21/24 Request a Quote
ZYXWVU 10/21/21 - 12/21/24 Request a Quote
Jo_Pitts1
Level 10 - Community Advisor

Re: Email Scripting - Filtering Unique Records

Is the list ordered?

i.e. once I've displayed a serial number, can I safely never display it again.  Or are their other rules in place?

Regards

Jo

Amey_Shivapurka
Level 2

Re: Email Scripting - Filtering Unique Records

Yes, it is ordered by purchase date descending and it is filtered by just assets that were created yesterday. The duplicate serial numbers are being created when the customer has purchased a product coverage (which is always purchased after the original asset is created).

 

So in the table example I shared you'll never see the serial number more than twice.

Jo_Pitts1
Level 10 - Community Advisor

Re: Email Scripting - Filtering Unique Records

So is this as simple as simply NOT displaying those records that have Request a Quote in Product Protection?

 

Amey_Shivapurka
Level 2

Re: Email Scripting - Filtering Unique Records

No that won't work because there could be case where the customer has an asset, but no product coverage and so they would just see that one. So this issue of duplicates serial number only happens when the customer has coverage

SanfordWhiteman
Level 10 - Community Moderator

Re: Email Scripting - Filtering Unique Records

Sounds like you’re overcomplicating.

 

To display only one item for each unique String value of a certain property, make that property the unique key of a new map.

  • create an empty map ( #set( $uniques = {} ) )
  • loop over the list, check if there’s already a key (containsKey) with the prop value
    • if the key exists, continue
    • if the key doesn’t exist, put the current item in the map with that key

Now you have a map with one item for each unique value. Iterate over its values().

Jo_Pitts1
Level 10 - Community Advisor

Re: Email Scripting - Filtering Unique Records

@SanfordWhiteman ,

you beat me to it.  I was going to say exactly the same thing, now we are down to the core requirement.

 

Cheers

Jo