Velocity Script Sorting CO Dates with Null Values | Community
Skip to main content
New Participant
September 21, 2021
Question

Velocity Script Sorting CO Dates with Null Values

  • September 21, 2021
  • 1 reply
  • 1779 views

@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?
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

1 reply

Jo_Pitts1
Community Manager
September 22, 2021

@ajay_mamgain12 ,

In @sanfordwhiteman 's post that you referenced: https://nation.marketo.com/t5/product-discussions/custom-object-multi-level-ordering-sorting/m-p/61216 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

New Participant
September 23, 2021

@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
New Participant
September 23, 2021

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.