Need to retrieve the second product category in the list | Community
Skip to main content
New Participant
December 1, 2020
Solved

Need to retrieve the second product category in the list

  • December 1, 2020
  • 1 reply
  • 2895 views

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

 

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by SanfordWhiteman

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.)

1 reply

SanfordWhiteman
New Participant
December 1, 2020
  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?

swedenmAuthor
New Participant
December 1, 2020

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
SanfordWhitemanAccepted solution
New Participant
December 2, 2020

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.)