How to Automatically Organize Products into Folders
In the Quoting module, folders can be logically created based on product attributes.
Each time a user adds products, the code will run and automatically place the items in the folders.
In this example, the folders are based on Product Class.
The following code will be in the Quote header logic:
Ensure that the code only runs in the pre-phase. Items cannot be deleted or moved in the post-phase.
Find items in the quote that are products and are not already present in the folders. We do not want to touch the items that are already sorted.
def skus = qp.getQuoteView().lineItems?.findAll{!it.folder && !it.parentId}?.sku
Search for the Product Class of each item that meets the above criteria.
def prodSrc = [] def result = "start" while (!result.isEmpty()){ result = api.find("P",startRow,rowLimit,"",["sku","attribute3"],Filter.in("sku",skus)) ?: [] startRow += rowLimit result?.each{prodSrc.add(it)} }
Go in cycles through the line items and delete each of them. They will be re-added later.
qp.getQuoteView().lineItems.each{ if (!it.folder && !it.parentId) { qp.deleteItem(it.lineId) } }
Create a new QuoteStructure to add to the quote later.
Determine which folders to create. Some Product Classes may already have folders and we do not want duplicates.
Find the added SKUs associated with the particular Product Class.
If the folder needs to be created, add the items to the folder's QuoteStructure which you have created earlier. Be sure to use createPart, as addPart will cause the code to run again and result in duplicate items.
If the folder already exists, find the folder's line id, and add the items to the folder. Alternatively, you could detect this condition earlier and use moveItem instead of deleting.
Finally, add the QuoteStructure to the quoteProcessor item.
The end result will look like this:
The full code:
Found an issue in documentation? Write to us.