Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

In this section you will learn more about Customs Forms.

Lab Info

Lesson: Product Discount Data Management (Standalone Custom Form)

Target Audience: Certified Configuration Engineer

Estimated Time to complete: TBD

Requirements & Solution Author: Petr Rys

...

Create a standalone Custom Form to help pricing managers manage discounts per Discounts, Product Groups and Families from one place.
Product Group is part of Product Master, Product Family is assigned per Product Group.

Each Product group belongs to 1 Product Family as per table . (Product Family Mapping . table)
Based on cumulative yearly revenue on the whole Product Family the customer has assigned Discount Level in Discount Level Definition Table.
Based on Product Group and Discount Level achieved by the Customer the sales agent has a "recommended discount" and a "maximum allowed discount" (Target Discount % and Max Discount %)

...

Product Discount Data Management Custom Form allows users to perform these tasks:

  • Change Data → select a table and change values within the table without adding new rows.

  • Add new Product Group → add a new group, assign it to a product family, and assign all the required discounts for different levels. It is important that the discount for each additional level is greater than or equal to the discount for the previous level; otherwise, the validation process will fail.

  • Add a product family → reassign product groups to reflect a new product family.

  • Add new discount level to existing product family → create new discount levels for an existing product family. The levels must be continuous, starting from level 1 at 0 Min Revenue. Each additional level must have a minimum revenue greater than or equal to the previous level. Users can assign new discounts for these new levels, ensuring that the discount for each additional level is greater than or equal to the discount for the previous level; otherwise, the validation will fail.

...

  • Create Standalone Custom Form, Custom Form Type

  • Utilize Input Matrix as Custom Form input

  • Understand Custom Forms Workflow

Provided resources

Logics:

Logics:

View file
namesCFO_ProductDiscountLib.rar
(sCFO_ProductDiscountLib)

Company Parameters:

View file
namePricingParameter.rar
(ProductFamilyMapping, DiscountLevelDefinition, Discount)

Acceptance Criteria

  • User can access a standalone Custom Form within the Analytics section to complete tasks listed in the Details section.

...

Expand
titleDive Deeper: Check out a step by step flow of the sample solution video below
  1. To create a standalone custom form, go to the main menu.

  2. Locate the 9 dots icon in the left upper corner of application. Click to open the drop down.

  3. Go to Analytics > Product Discount Manager.

  4. In the new dialogue window, click on Create New Product Discount Manager icon in the top right corner.

  5. Fill in the label then click on the blue Add icon in the bottom right corner of the window.

(tick) You will get a confirmation message in green that the form has been added successfully.

  1. In the following window, under Inputs, select the management option from the drop down menu.

  2. Depending on your choice, continue filling out the remaining fields.

  3. To assign new products, click on the box and choose where you want to assign from the drop-down menu.

  4. Click on Add in the bottom left corner to add discount levels if applicable.

(warning) Note that you may have to check what values are assigned to each level. To avoid missing out any fields, the system displays warning icons which disappear once the field has been filled out.

(info) You can add as many discount levels as you need. Check out our documentation to learn more about discounts.

  1. Click on Recalculate in the upper right corner.

  2. To see a more detailed view of the Calculations, use the arrow on the right side next to the reload icon to open the calculations window. Click on Show to learn more.

  3. To close the dialogue window click on X in the top right corner.

  4. Click Submit in blue in the upper right corner to send your form for approval. A confirmation message in green will appear at the top center of the window.

  5. In the Workflow tab, if you have enough rights for the operation, you can approve the form by clicking on the green checkmark icon. You can also add a reason for both approvals or rejections.

  6. Add your reason, then click Submit. You will get another confirmation message.

(info) If you go to Company Parameters by clicking the third icon from the left in the top right corner, under Categories> Custom Form, you can check your newly created form.

...

CF-Sample Solution.mp4

Step-by-step solution

Develop the logic and test in Studio

...

  • AbortOnInputGenerationExecution

Code Block
languagegroovyjava
if (api.isInputGenerationExecution()) {
    api.abortCalculation()
}
  • BuildStep

Code Block
languagegroovyjava
workflow.addWatcherStep("Sales Manager")
        .withUserWatchers("admin")
        .withReasons("Sales manager needs to approve this form")

workflow.addApprovalStep("Sales Manager")
        .withPostStepLogic("customform_WorkflowPostStep")
        .withApprovers("admin")
        .withReasons("Sales manager needs to approve this form")

...

  • UpdateData

Code Block
languagegroovyjava
if (workflowHistory.steps.findAll { it.approvalStep }.every { it.approved } || 
workflowHistory.activeStep?.isEmpty()) {
        //TODO: The logic that will be performed after the user approved all the steps.                                    
}

...

  1. Proceed with deploying all logic created logics to your partition.

  2. Access Administration → Custom Form Types from your partition. Create a new form type by clicking on the [+ New Form Type] button located at the top-right corner of the screen.​ Uncheck Embedded option and fill in the form:

If you miss [+ New Form Type] button, add Custom Forms rights to your user (admin) using User Admin.

...

Add the following code in the Configuration section.

...

In this configuration, the first tab is named "details" and uses the "dashboard" icon. It has a name that can be translated using the "Management options" key. The second tab is named "workflow" and uses the "process" icon. It has a name that can be translated using the "Workflow" key. The third tab is named "actions" and uses the "file-check-alt" icon. It has a name that can be translated using the "Todo list" key. The step label itself is also translatable using the "Product Discount Data Management" key.

Pricefx Studio - Create Custom Form Type

Using the Create Custom Form Type feature in Pricefx Studio simplifies the process of creating a Custom Form Type as compared to manual creation through the user interface. To create a custom form type in Pricefx, please follow these steps:

...

  1. Navigate to the sCFO_ProductDiscountDataMgmn logic and open the Configurator.groovy file. Once you have accessed the file, add the following code snippet to it.

Code Block
languagegroovyjava
if (customFormProcessor.isPrePhase()) {
    
    //1. Create two variables named 'configuratorName' and 'cfoFormIdKey'. Assign values to them from the Constconfig file.
    //String configuratorName = 
    //String cfoFormIdKey = 

    //2. Assign value to currentCfoForm. Use api.currentItem()
    //def currentCfoForm = 
    
    //3. Get id from current Item. Remember that current item can be null.
    //Long cfoFormId = 

    //4. Get inputs from currentCfoFrom.
    //5. Find input with a name that matches configuratorName
    //6. Take value  
    //7. If the above operation returns null, return an empty map [:]. You can use elvis operator '?:'
    //If you want to prevent NullPointerException, it is recommended to use the safe navigation operator '?'. This will ensure that your code doesn't throw an exception if a null value is encountered.
    //Map configuratorValueBackup = 
    
    //8. Add current form id to the configuratorValueBackup. Use cfoFormIdKey as a key and cfoFormId as value.
    //configuratorValueBackup.put()

    //9. update the input "ROOT" with a map that provides the name, label, url, type and value.
    customFormProcessor.addOrUpdateInput("ROOT",
            [name : configuratorName,
             label: configuratorName,
             url  : configuratorName,
             type : InputType.INLINECONFIGURATOR,
             value: configuratorValueBackup]
    )

}

Take a look at line 4 and the value of configuratorName, which will be created in the next step.

DEBUGINGDEBUGGING

Make sure to set the proper CustomForm for your logic in the Inputs tab. Run and test the logic afterwards.

...

In this step, we will create the configuratorEntry object and add a new hidden input with our custom form id to the configuratorEntry before returning it.

Code Block
languagegroovyjava
import net.pricefx.server.dto.calculation.ConfiguratorEntry

// Create a key for the CFO form ID from specified library
//String cfoFormIdKey = 

// Create a new ConfiguratorEntry using the API
//ConfiguratorEntry configuratorEntry = 

// Create a HiddenEntry InputBuilder with the CFO form ID key and build a context parameter for it
//def param = 

// Add the newly created parameter to the ConfiguratorEntry. Use createParameter method.

// Return the modified ConfiguratorEntry

...

In this step, we will add a header and a drop-down list with options for selecting individual forms to the configurator.

Code Block
languagegroovyjava
// Initialize a constant for main paragraph message
//String PARAGRAPH_MESSAGE_MAIN = 

// Get value of the first input from the ConfiguratorEntry, only if it exists
Long formId = 

// Get the product discount configurator utility script from the product discount library
Script productDiscountConfigurator = 

// Instantiate the main entry of the product discount configurator. 
// Use getMainConfiguratorEntry method with formId as a parameter
ConfiguratorEntry mainConfiguratorEntry = 

// Set the paragraph header for the main configurator entry. Use the constant initialized earlier

// Return the main configurator entry

...

In this step, we retrieve the option selected by the user and pass it as a parameter to the method that changes the form depending on the selected option.

Code Block
languagegroovyjava
import net.pricefx.server.dto.calculation.ConfiguratorEntry

// Get value of the first input from the OtionInput.groovy, only if it exists
String selectedType = 
// Use configuratorSwitch method. Pass selectedType as a parameter.

/**
 * This method is used to select and configure different types of ConfiguratorEntries.
 * It also loads the correct messages for each type.
 *
 * @param selectedType The type of ConfiguratorEntry to be used. This could be 'CHANGE_DATA', 'NEW_FAMILY', or 'NEW_GROUP'.
 * @return A ConfiguratorEntry instance configured based on the selectedType.
 * If the getterMethod or message for the selectedType do not exist, the method will return null.
 */
ConfiguratorEntry configuratorSwitch(String selectedType) {
    Script CONFIG_UTILS = libs.sCFO_ProductDiscountLib.CfoConfiguratorUtils
    Script CONST_CONFIG = libs.sCFO_ProductDiscountLib.ConstConfig

    def getterMap = [
            (CONST_CONFIG.CHANGE_DATA) : CONFIG_UTILS.&getChangeDataConfiguratorEntry,
            (CONST_CONFIG.NEW_FAMILY)  : CONFIG_UTILS.&getAddFamilyConfiguratorEntry,
            (CONST_CONFIG.NEW_GROUP)   : CONFIG_UTILS.&getAddGroupConfiguratorEntry,
    ]

    def messageMap = [
            (CONST_CONFIG.CHANGE_DATA) : CONST_CONFIG.CFO_CONFIGURATOR_CONFIG.PARAGRAPH_MESSAGE_CHANGE,
            (CONST_CONFIG.NEW_FAMILY)  : CONST_CONFIG.CFO_CONFIGURATOR_CONFIG.PARAGRAPH_MESSAGE_ADD_FAMILY,
            (CONST_CONFIG.NEW_GROUP)   : CONST_CONFIG.CFO_CONFIGURATOR_CONFIG.PARAGRAPH_MESSAGE_ADD_GROUP,
    ]

    def getterMethod = getterMap[selectedType]
    def message = messageMap[selectedType]

    if (getterMethod && message) {
        return configureEntries(getterMethod, message)
    }
}

/**
 * This method applies configuration to entries.
 *
 * @param getterMethod Method reference for getting configurator entry.
 * @param message The message to be set as paragraph header in configurator entry.
 * @return ConfiguratorEntry The modified configurator entry with new paragraph header.
 */
ConfiguratorEntry configureEntries(def getterMethod, String message) {
    ConfiguratorEntry configuratorEntry = getterMethod.call()
    configuratorEntry.setMessage(message)
    return configuratorEntry
}

...

  • Open logic.json in sCFO_ProductDiscountHeader_Configurator.

  • Open Studio Editor.

  • Click on the Inputs tab.

  • Set the Context to CONFIGURATOR.

  • Test LogicGenerate Inputs.

  • Click on the Results tab and check output.

Your output should resemble the example given below.

...

For Discount Level Definition option:

...

CF-Change Data Discount Level Definition.

...

mp4

For Product Family Mapping option:

...

CF-Change Data Product Family Mapping.

...

mp4

For Discount option:

...

CF-Change Data Discount.

...

mp4

  1. All fields are required.

...

  • Add new Product Group option

...

  1. “New Product Goup“ is an input type of String

  2. “Assign Product Family” is a drop-down list with unique values of Product Familty colum in Discount Level Definition company parameter table.

  3. The table for adding and deleting discount records should have identical columns as the Discount company parameters table, and provide a user-friendly way to add records.

  4. “Product Group” column shoud have a drop-down list with newly added “New Product Group”. ColumnType: Option

  5. “Discount Level” coulumn should have a drop-down list with list of numbers from 1 to 6. columnType: Option

  6. “Traget Discount” and “Max Discount” are coulmns with columnType: Numeric

  7. All fields are required.

...

...

  • Add new Product Family option

...

  1. “New Product Family“ is an input type of String

  2. “Assign new Product Family to an already existing Product Group.” is a checkbox. Default: unchecked.

  3. “Assign to:” is a drop-down list. This is a list of unique values from “Product Family” column in Discount Level Definition company parameters table. This filed will be visible if the “Assign new Product Family to an already existing Product Group” value is set to true (with the checkbox checked).

  4. The table for adding and deleting new discount levels records should have identical columns as the Discount Level Definition company parameters table, and provide a user-friendly way to add records.

  5. “Product Family” column shoud have a drop-down list with newly added “New Product Family”. ColumnType: Option

  6. “Discount Level” and “Min Revenue” are coulmns with columnType: Numeric

  7. All fields are required.

scenario1 CF-Add new New Product Family.webmmp4

...

Solution

Go to ChangeDataInputs.groovy and look at the configuratorSwitch method. Implement methods for each option chosen by the user to create one of three forms. Each method returns the ConfiguratorEntry object.

Code Block
languagegroovyjava
/* Fragment of configuratorSwitch method */
def getterMap = [
            (CONST_CONFIG.CHANGE_DATA) : CONFIG_UTILS.&getChangeDataConfiguratorEntry,
            (CONST_CONFIG.NEW_FAMILY)  : CONFIG_UTILS.&getAddFamilyConfiguratorEntry,
            (CONST_CONFIG.NEW_GROUP)   : CONFIG_UTILS.&getAddGroupConfiguratorEntry,
    ]
  • getChangeDataConfiguratorEntry()

Code Block
languagegroovyjava
ConfiguratorEntry getChangeDataConfiguratorEntry() {
    ConfiguratorEntry changeDataSection = api.createConfiguratorEntry()
    //TODO: create changeDataSection
    //1. Get config map from libraray (CFO_CONFIGURATOR_CONFIG)
    //2. Create option input - name: "ppTableInput", label: "Select table", required: true. 
    //   Get the value from CFO_CONFIGURATOR_CONFIG by OPTIONS.TABLE_OPTIONS key and
    //   pass it as options parameter.
    //3. Get value from ppTableInput 
    //4. Display a specific group of fields based on the option that is chosen.
    //   Check required inputs in follow sections:
    //   - For Discount Level Definition option
    //   - For Product Family Mapping option
    //   - For Discount option
    return changeDataSection
}

...

  • getAddFamilyConfiguratorEntry()

Code Block
languagegroovyjava
ConfiguratorEntry getAddFamilyConfiguratorEntry() {
    ConfiguratorEntry productFamilySection = api.createConfiguratorEntry()
    //TODO: create productFamilySection
    //1. Create two inputs:
    //   a) InputType.STRINGUSERENTRY, name: "productFamilyInput", label: "New Product Family", required: true
    //   b) InputType.BOOLEAN, name: "assignToGroup", label: "Assign new Product Family to an already existing Product Group."
    //2. Get input value from "assignToGroup". If value is true:
    //   a) Find unique family names from the ProductFamilyMapping company parameters table.
    //   b) Create option input - name: "assignProductGroup", label: "Assign to", pass values from step a)
    //3. Create a table. Use result from step 1a as a option for product family option.
    return productFamilySection
}

...

  • Assign to: list of unique names from ProductFamilyMapping lookup table

  • New Discount Levels: user can add and delete rows.

...

  • getAddGroupConfiguratorEntry()

Code Block
languagegroovyjava
ConfiguratorEntry getAddGroupConfiguratorEntry() {
    ConfiguratorEntry productGroupSection = api.createConfiguratorEntry()
    //TODO: create productGroupSection
    //1. Find unique family names from the ProductFamilyMapping company parameters table. 
    //   You can use the API function "findLookupTableValues".
    //2. Create two inputs:
    //   a) InputType.STRINGUSERENTRY, name: "productGroupInput", label: "New Product Group", required: true 
    //   b) InputType.Option, name: "Assign Product Family", label: "assignProductFamily", required: true
    //      Pass the values from first step as a setValuesOptions() parameter
    //3. Get input value from productGroupInput
    //4. Create a table. Use result from step 3 as a option for product group option.      
    return productGroupSection
}

...

  • Assign Product Family: list of unique values from ProductFamilyMapping lookup table

  • New Discount: user can add and delete rows

...

Calculations

The results will be displayed as tables in the right panel. Only tables with changes will be shown as copies of parameter tables with updated values.

...

  1. Open the Calculation.groovy file, which is located in the sCFO_ProductDiscountDataMgmt logic. Once it's open, please add the following code to the file.

Code Block
languagegroovyjava
if (customFormProcessor.isPostPhase()) {
    // choosenOption is a value from managementOptionsInput
    if (chosenOption == "Add new Product Group") {
        //TODO: Part-1
    } else if (chosenOption == "Add new Product Family") {
        //TODO: Part-2
    } else if (chosenOption == "Change data") {
        //TODO: Part-3
        if (ppTableInput == "Product Family Mapping") {
            //TODO: Part-4
            if (chosenTable == "Product Family") {
            //TODO: Part-5    
            } else if (chosenTable == "Product Group") {
            //TODO: Part-6  
            } 
            //TODO: Part-7
        } else if (ppTableInput == "Discount Level Definition") {
            //TODO: Part-8  
        } else if (ppTableInput == "Discount") {
            //TODO: Part-9
        }
    }
    
}

...

Task

Sample Code

Create a table

Code Block
languagegroovyjava
ResultMatrix newTable = api.newMatrix('Column_1', 'Column_2',)
newTable.setColumnFormat('Column_1', FieldFormatType.TEXT)
newTable.setColumnFormat('Column_2', FieldFormatType.NUMERIC)

Get data from company parameter table

Code Block
languagegroovyjava
def getDiscount() {
    def id = api.findLookupTable("Discount").id
    def filters = [ Filter.equal('lookupTable.id', id) ]
    def discountTable = api.find('MLTV2', 0, 0, null, *filters)

    def rows = discountTable.collect {
        [
                productGroup  : it.key1,
                discountLevel : it.key2,
                targetDiscount: it.attribute1,
                maxDiscount   : it.attribute2,
                typedId       : it.typedId]
    }
    return rows
}

How to edit cell style in table

Code Block
languagegroovy
def cell_1 = newTable.styledCell(example_value, "black", "yellow", "bold")
def cell_2 = newTable.styledCell(example_value, "black", "yellow", "bold")
newTable.addRow(cell_1, cell_2)

How to add new table to the customFormProcessor.

Code Block
languagegroovyjava
customFormProcessor.addOrUpdateOutput(
        [
                "resultName" : "new_name",
                "resultLabel": "new_label",
                "resultType" : "MATRIX",
                "result"     : newTable,
        ]
)

...

Saving changes to the tables

...

We need to ensure that changes in the target tables are displayed accurately. Once the user approves the changes, they should be saved. The customform_WorkflowPosStep logic is responsible for this process.

We need to revisit the logic that controls our Workflow. Our logic consists of two steps. Take a look at BuildStep.groovy

Code Block
languagegroovyjava
workflow.addWatcherStep("Sales Manager")
        .withUserWatchers("admin")
        .withReasons("Sales manager needs to approve this form")

workflow.addApprovalStep("Sales Manager")
        .withPostStepLogic("customform_WorkflowPostStep")
        .withApprovers("admin")
        .withReasons("Sales manager needs to approve this form")

...

This task can be approached in various ways. You can either attempt to preserve the data or use the provided solution below.

Code Block
languagegroovyjava
if (workflowHistory.steps.findAll { it.approvalStep }.every { it.approved } || workflowHistory.activeStep?.isEmpty()) {
    //part-1
    if (optionInput == "Change data") {
        if (changeGroupOrFamily == "Product Group") {
        //part-2
        } else if (changeGroupOrFamily == "Product Family") {
        //part-3
        } else if (ppTableInput == "Discount") {
        //part-4
        } else if (ppTableInput == "Discount Level Definition") {
        //part-5
        }
    } else {
        //part-6
        if (optionInput == "Add new Product Family") {
        //part-7
        } else if (optionInput == "Add new Product Group") {
        //part-8
        }
    }
}

...

Task

Code samples

Get user inputs from current item assign it to inputs variable

Code Block
languagejava
api.currentItem()?.inputs?.get(0)?.value

Get individual field values from user inputs:

Code Block
languagejava
inputs?.get("managementOptionsInput")

Get "Discount" lookup table Id assign it to the discountID variable.

Code Block
def discountID = api.findLookupTable("Discount").id

Create filters:

  • 'ProductGroup' equal oldName

  • 'lookupTable.id' equal discountID

Code Block
languagejava
def discountFilters = [
        Filter.equal("ProductGroup", oldName),
        Filter.equal('lookupTable.id', discountID)
]

Update "ProductFamilyMapping" lookup table. To obtain accurate data, make use of filters.

Code Block
languagejava
api.find('LTV', 0, *discountFilters).collect {
    def entry = [
            lookupTableName: "ProductFamilyMapping",
            id             : it.id,
            valueType      : it.valueType,
            typedId        : it.typedId,
            name           : newName,
            value          : it.value
    ]
    api.addOrUpdate("LTV", entry)
}

Use discountEntries and update "Discount" lookup table

Code Block
languagejava
discountEntries.each {
    api.addOrUpdate("MLTV2", [
            lookupTableName: "Discount",
            key1           : it["Product Group"].value,
            key2           : it["Discount Level"].value,
            attribute1     : it["Target Discount %"].value,
            attribute2     : it["Max Discount %"].value
    ])
}

Get "DiscountLevelDefinitionMatrix" table outputs from current item

Code Block
languagegroovyjava
api.currentItem()?.outputs?.find { it.resultName == "DiscountLevelDefinitionMatrix" }

If output is null get an empty list

Code Block
languagegroovyjava
discountLevelDefinitionTable = discountLevelDefinitionTable ? discountLevelDefinitionTable : []

Filter the rows in their respective tables based on the values of Map.

Code Block
languagegroovyjava
def discountLevelDefinitionEntries = discountLevelDefinitionTable.result.entries.findAll { it['Product Family'] instanceof Map }

Resources for Further Learning

References

Custom Forms (Reference)

Custom Forms Configuration How-To(s)

...

Documentation

Custom Forms

Custom Forms General Settings

Custom Forms Header Logics

Company Parameters

Groovy API

setConfigParameter

Other

...

Info

NOTE: If you have any inquiries regarding the content in this learning path, you can utilize the following online Pricefx forums: AskPricefxcommunity or Pricefx GenAI chatbot.