Working with QuoteStructure

QuoteStructure can be used to add line items to a quote using Groovy code. This helps in some use cases, such as importing quote line items from an Excel file.

From version 2.2.0, line items added using QuoteStructure can be handled by the CPQ Accelerator. This section will show you how to work with QuoteStructure in CPQ.

Utils

CPQ provides some public utils to make it easier to work with the QuoteStructure:

  • void addQuoteStructure(QuoteStructure quoteStructure) – Loads the provided quoteStructure so that the CPQ knows which line items are going to be loaded and calls quoteProcessor.addQuoteStructure(QuoteStructure quoteStructure) to add quoteStructure into the system.

  • void addLineItem(String parentLineId, String key, List<Map<String, Object>> contextParameter) –Loads the provided line item so that the CPQ knows which line item is going to be loaded and calls quoteProcessor.addLineItem(String parentLineId, String key, List<Map<String, Object>> contextParameter) to the add line item into the system.

  • void addLineItem(String key, List<Map<String, Object>> contextParameter) – Loads the provided line item so that the CPQ knows which line item is going to be loaded and calls quoteProcessor.addLineItem(String key, List<Map<String, Object>> contextParameter) to add the line item into the system.

CPQ requires QuoteStructure / Line Items added using Groovy to be loaded in the pre-phase so that the inputs and outputs are generated correctly.

It is recommended in the Data Processing / Query with the running phase to set it to PrePhase.

Samples

This example shows a use case where a user uploads an Excel file with line items and adds them to a quote.

You need to configure a file input on the header that allows the user to upload the Excel file. You need to load the Excel only once when the user uploads it, for that use the provided library CPQ_Inputs_Utils.Void which removes the user input with every recalculation.

Result in the quote:

Then, you need to write a library to process the Excel file and add line items using QuoteStructure. Create a library and implement it, with the ImportExcel element in the CPQ_Header_Utils library. Of course, you are encouraged to separate CPQ core logic and custom logic.

import net.pricefx.common.apibuilder.quote.QuoteStructure def execute() { List<List> rows = getExcelData() if (rows?.size() < 2) { return } QuoteStructure quoteStructure = buildQuoteStructure(rows) libs.CPQ_SharedLib.QuoteUtils.addQuoteStructure(quoteStructure) } protected List<List> getExcelData() { def quoteUtils = libs.CPQ_SharedLib.QuoteUtils String excelImportInputName = "ImportExcel" //Configured header input name String importedFileHandler = quoteUtils.getLineItemInputMap("ROOT")[excelImportInputName] def excelData = api.parsableInputFileDataFromHandle(importedFileHandler) return excelData?.data?.Data } protected QuoteStructure buildQuoteStructure(List<List> rows) { int skuIndex = 0 int colorIndex = 1 def quoteStructure = new QuoteStructure() List rowLabels = rows[0] //Remove the label row rows = rows?.subList(1, rows?.size()) String colorName = rowLabels[colorIndex] String colorLabel = colorName.capitalize() rows.each { List row -> String sku = row[skuIndex] String color = row[colorIndex] List inputs = [["name": colorName, "label": colorLabel, "value": color]] quoteStructure.addPart(sku, inputs) } return quoteStructure }

The above logic simply reads the Excel file from the input, reads its data to build QuoteStructure, then uses the quote utils to apply the QuoteStructure.

After the logic is created, you configure it to run the library in CPQ.

Now, you can upload the Excel file and recalculate it to see if the new line items from the file are added to the quote.

Â