I want to do the following
If the lead has the date data of"Field93__c" in $OpportunityList, I would like to show 2 month after date of it.
If they do not have, I would like to show the text "Contact Customer Support".
I created the following script token
#set( $defaultTimeZone = $date.getTimeZone().getTimeZone("Asia/Tokyo") )
#set( $defaultLocale = $date.getLocale() )
#set( $calNow = $date.getCalendar() )
#set( $ret = $calNow.setTimeZone($defaultTimeZone) )
#set( $calConst = $field.in($calNow) )
#set( $ISO8601DateOnly = "yyyy-MM-dd" )
#set( $ISO8601DateTime = "yyyy-MM-dd'T'HH:mm:ss" )
#set( $JapanDate = "yyyy年MM月dd日")
#set( $ISO8601DateTimeWithSpace = "yyyy-MM-dd HH:mm:ss" )
#set( $ISO8601DateTimeWithMillisUTC = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" )
#set( $ISO8601DateTimeWithMillisTZ = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" )
#set( $sortedList = $sorter.sort($OpportunityList,"Field93__c"))
#set( $dateObj = $convert.toCalendar(
$convert.parseDate(
$sortedList.get(0).Field93__c,
$ISO8601DateOnly,
$defaultLocale,
$defaultTimeZone
)
) )
#if($dateObj)
$dateObj.add($calConst.MONTH, 2)
${date.format($JapanDate, $dateObj, $defaultLocale, $defaultTimeZone
)}
#else
Contact Customer Support
But it does not work. How should i correct ?
Solved! Go to Solution.
The flaw in this code is that you can’t sort a null value.
If there’s > 1 Opportunity and the value of
Field93__c
is null for any item in
$OpportunityList
the sort errors out and $sortedUpdated is never set (it is also null).
For handling this, would you recommend iterating over the list and filling in any null with an empty-like value of the same type (in this particular case the date type) first?
Nope, because that disrupts order (empty strings sort first but null doesn’t mean “earliest”).
If I know a field is nullable, I usually do a first pass over the items and move only the sortable ones to another list, then sort that list.
Could you try the below script:
#set( $defaultTimeZone = $date.getTimeZone().getTimeZone("Asia/Tokyo") )
#set( $defaultLocale = $date.getLocale() )
#set( $calNow = $date.getCalendar() )
#set( $ret = $calNow.setTimeZone($defaultTimeZone) )
#set( $calConst = $field.in($calNow) )
#set( $ISO8601DateOnly = "yyyy-MM-dd" )
#set( $ISO8601DateTime = "yyyy-MM-dd'T'HH:mm:ss" )
#set( $JapanDate = "yyyy年MM月dd日")
#set( $ISO8601DateTimeWithSpace = "yyyy-MM-dd HH:mm:ss" )
#set( $ISO8601DateTimeWithMillisUTC = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" )
#set( $ISO8601DateTimeWithMillisTZ = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" )
#set ($sortedUpdated = $sorter.sort($OpportunityList,"Field93__c"))
#if($sortedUpdated[0].Field93__c && !$sortedUpdated[0].Field93__c.isEmpty())
#set( $inputDate = $convert.toCalendar(
$convert.parseDate(
$sortedUpdated[0].Field93__c,
$ISO8601DateOnly,
$defaultLocale,
$defaultTimeZone
)
))
$inputDate.add($calConst.MONTH,2)
${date.format(
$JapanDate,
$inputDate,
$defaultLocale,
$defaultTimeZone
)}
#else
Contact Customer Support
#end
The flaw in this code is that you can’t sort a null value.
If there’s > 1 Opportunity and the value of
Field93__c
is null for any item in
$OpportunityList
the sort errors out and $sortedUpdated is never set (it is also null).
Gotcha! That's a very valid point- for most of my use cases, I usually have to sort on the standard updatedAt field so I don't run into that error. I do recollect that we can't mix types with a generic SortTool.sort, and comparing a null with a Date value, a Boolean with a String, or a Number with a Boolean, are all mixed types, and hence would run into errors. For handling this, would you recommend iterating over the list and filling in any null with an empty-like value of the same type (in this particular case the date type) first?
For handling this, would you recommend iterating over the list and filling in any null with an empty-like value of the same type (in this particular case the date type) first?
Nope, because that disrupts order (empty strings sort first but null doesn’t mean “earliest”).
If I know a field is nullable, I usually do a first pass over the items and move only the sortable ones to another list, then sort that list.