Velocity Script Sorting CO Dates with Null Values

Ajay_mamgain12
Level 1

Velocity Script Sorting CO Dates with Null Values

@SanfordWhiteman I was going through some of your old discussions in community-related to sorting dates if there are null values included and you did mention you are going to publish an article soon with details. 

 

 
Can you share the article link because I'm facing a similar issue and as I want to sort dates from a custom object which includes null values as well?
3 REPLIES 3
Jo_Pitts1
Level 10 - Community Advisor

Re: Velocity Script Sorting CO Dates with Null Values

@Ajay_mamgain12 ,

In @SanfordWhiteman 's post that you referenced: https://nation.marketo.com/t5/product-discussions/custom-object-multi-level-ordering-sorting/m-p/612... he gives the answer.

 

I quote:


 

What you can do in the interim, and I hate to recommend it but have no choice right now, is loop over the list once and transform the null values into a representative value with the expected type:

 

#foreach( $item in $List_cList )
#if( !$item.Availability.class )
#set( $item.Availability = true )
#end
#if( !$item.availabilityUpdated.class )
#set( $item.availabilityUpdated = "0000-00-00" )
#end
#end

 

This works by checking to see if the values have a class. nulls won't have a class, so you can then set them to what you want the default to be. Then sort after that.

You can see he 'de-nulls' the dates by testing for a null class.  You can probably leverage that to achieve your desired goal

Once you've done that, you then sort as per the other references in the same post:

 


$sorter.sort($List_cList,["availabilityUpdated:desc","Availability:desc"])

Cheers

Jo

Ajay_mamgain12
Level 1

Re: Velocity Script Sorting CO Dates with Null Values

@Jo_Pitts1 Thank you for the recommendation I did try the code however I'm not getting the list sorted by it.

 

#foreach( $item in $OpportunityList )
#if( !$item.mag_inttakeenddate.class )
#set( $item.mag_inttakeenddate = "0000-00-00" )
#end
#end

#set($oppList=$sorter.sort($OpportunityList, "mag_inttakeenddate:desc"))

 Do you have any other recommendations? It just shows the list without sorting the values. I can see that it has replaced nulls with 0000-00-00 however sorting is somehow not working as expected.

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity Script Sorting CO Dates with Null Values

First: $sorter.sort returns a new list, so make sure you’re using $oppList, not the original list.

 

Second: what sort order do you actually want? Ascending order would place the previously-null items at the top.