SOLVED

Need to retrieve the second product category in the list

Go to solution
swedenm
Level 2

Need to retrieve the second product category in the list

Hi There! I would like to ask for some help regarding a script we are currently working on. This script works however there are some cases in which the order date is the same with other product category which result to error. The error occur for this example, I have data with Stickers and Postcards order at the same time but I just need to show the data for postcards which is in second list.

 

Sample data:

Itemlist 1 - Stickers - Order date: Oct 9, 2020

Itemlist 2 - Postcards - Order date: Oct 9, 2020

Itemlist 3 - Postcards - Order date: Sept 4, 2020

 

My question would be, how can I retrieve a data in Itemlist 2 using the lastindex value since I need to still get the recent purchase for a specific product.

#set($product = "Postcards")
#set($size = $jobItemList.size())
#set($lastIndex = $jobItemList.size() - 1)
#set($joID = ${jobItemList.get($lastIndex).jobOrderId})


#foreach( $item in $jobItemList )


#if ($item.productCategory == $product && $item.jobOrderId == $joID)
$item.jobOrderId
#break
#end


#end

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Need to retrieve the second product category in the list


What I am trying to do is to get the recent order in the jobItemlist which equals to the product value I declare -> #set($product = "Postcards"). Does I make sense?

Yes, and that's much easier than what your code is doing now! You must've gotten mixed up somewhere along the way.

 

You don't need to be seeking an index or anything like that. Just sort the list, match the interesting product, and break after you found it.

#set( $targetProduct = "Postcards" )
#if( !$jobItemList.isEmpty() )
#foreach( $jobItem in $sorter.sort($jobItemList,"orderDate:desc") )
#if( $jobItem.productCategory.equals($targetProduct) )
${jobItem.jobOrderId}
#break
#end
#end
#end

 

(Obvs. substitute the field name for Order Date where I have orderDate — I don't know what its name is in your particular instance.)

View solution in original post

4 REPLIES 4
SanfordWhiteman
Level 10 - Community Moderator

Re: Need to retrieve the second product category in the list

  1. Remember: don't use formal references unless you're outputting content.
  2. Remember: don't use the == operator — it's broken in obscure ways. Instead use .equals().

Please provide more of a business-level description of what you're trying to do. The code is not giving a complete picture. You're trying to traverse the list to find the last item ("last" when sorted on what property?) and then find the first item that has that last item's jobOrderID?

swedenm
Level 2

Re: Need to retrieve the second product category in the list

Thanks for that quick response Sandford.

What I am trying to do is to get the recent order in the jobItemlist which equals to the product value I declare -> #set($product = "Postcards").

 

Does I make sense?

SanfordWhiteman
Level 10 - Community Moderator

Re: Need to retrieve the second product category in the list


What I am trying to do is to get the recent order in the jobItemlist which equals to the product value I declare -> #set($product = "Postcards"). Does I make sense?

Yes, and that's much easier than what your code is doing now! You must've gotten mixed up somewhere along the way.

 

You don't need to be seeking an index or anything like that. Just sort the list, match the interesting product, and break after you found it.

#set( $targetProduct = "Postcards" )
#if( !$jobItemList.isEmpty() )
#foreach( $jobItem in $sorter.sort($jobItemList,"orderDate:desc") )
#if( $jobItem.productCategory.equals($targetProduct) )
${jobItem.jobOrderId}
#break
#end
#end
#end

 

(Obvs. substitute the field name for Order Date where I have orderDate — I don't know what its name is in your particular instance.)

swedenm
Level 2

Re: Need to retrieve the second product category in the list

Thanks a lot Sandford. Will give this a try 🙂