Background

We wanted to have a possibility to create quotes directly from the dashboard. In our case, it was Customer Insight Product Portfolio Dashboard where users can see Product Health and Performance. The idea is to select multiple products (for example 5 underperforming) in the matrix table and click a Create Quote button.

However, we do not currently have Groovy API to create a quote. But we can still use a REST API and bound call “hack” to do so.

The request format might not be straightforward, that is why we share this example.

How It Works

  1. The logic creates a request that adds customer, valid from/to and product to the quote. The quote is calculated but not saved yet.

  2. Another request takes the previous results and saves the quote.

  3. The quote is visible in Pricefx and can be modified further.

See the code snippet below:

def createQuote(String quoteName, String customerId, List<String> skus) {

    def saveUrl = "quotemanager.save"
    def addProductsUrl = "quotemanager.addproducts"

    def expiryDate = getValidToDateString()
    def targetDate = getValidFromDateString()

    def addProductRequest = """
    {
        "data":{
            "quote": {
                "inputs":[
                    {
                        "name": "Customer",
                        "label": "Customer",
                        "type": "CUSTOMER",
                        "value": "$customerId",
                        "valueHint": "${api.customer("Customer Name", customerId)}"
                    }            
                ],
                "outputs":[],
                "lineItems":[],
                "quoteType":"Quote",
                "userGroupEdit":null,
                "userGroupViewDetails":null,
                "expiryDate":"$expiryDate",
                "targetDate":"$targetDate",
                "externalRef":null,
                "version":null,
                "typedId":null,
                "uniqueName":null,
                "label":"$quoteName",
                "viewState":{}
                },
        "parent": null,
        "skus": ${api.jsonEncode(skus)}
        }
    }
    """

    //add items
    def addProductResponse = api.boundCall("localhost", addProductsUrl, addProductRequest, false)
    def addProductResponseStatusCode = addProductResponse.statusCode
    if (addProductResponseStatusCode != "200") {
        api.trace("error", null, addProductResponseStatusCode)
        return
    }

    def quoteData = addProductResponse?.responseBody?.response?.data?.getAt(0)

    if (!quoteData) return

    def saveQuoteRequest = """
    {
    "data":{
        "quote":${api.jsonEncode(quoteData)}
        }
    }
    """

    // save quote
    def saveResponse = api.boundCall("localhost", saveUrl, saveQuoteRequest, false)
    def saveResponseStatusCode = saveResponse.statusCode

    if (saveResponseStatusCode != "200") {
        api.trace("error", null, saveResponseStatusCode)
        return
    }

    return "OK"
}

String getValidFromDateString() {
    Calendar calendar = api.calendar()
    calendar.getTime().format("yyyy-MM-dd")
}

String getValidToDateString() {
    Calendar calendar = api.calendar()
    calendar.add(Calendar.YEAR, 1)
    calendar.getTime().format("yyyy-MM-dd")
}

Notes