Use Functions and Context Rules with the Standard Cart APIs

Consider the following scenario

Scenario 

  • Create a pricelist with the following products and prices.
    • Prod1 (Product2):
      • Onetime charge - $10(Price)
    • 200MBPS (Product2):
      • Onetime charge - $10(Price)
      • Onetime charge - $500(Price)

As you can see here, 200MBPS has $10 and $500 as one time price. Consider a use case where we can create context rules which validates if Prod1 is part of the cart or not.

  • If it is part of the cart, then the price for 200MBPS is $500
  • Else, price for 200MBPS is 10$

EPC Setup 

Perform the following.

Create a Product Catalog DC1 for the Use Case 

  • Ensure that catalog has following products.
    • Prod1
    • 200MBPS
  • Create following one time prices for each of the products.
    • Prod1
      • Onetime charge - $10
    • 200MBPS
      • Onetime charge - $10
      • Onetime charge - $500

DC1

Create a Class 

  • The following class defines context rules function ProductInOrder, which validates if a given product code is part of the cart or not
  • Ensure sure to update this class to support managed package by appending vlocity_cmt for the following global classes:
    • VlocityOpenInterface
    • CpqCartDocument
    • CpqCartDocumentItem
    • FlowStaticMap
1global with sharing class CtxRuleFunctionApex implements VlocityOpenInterface {
2
3    global Boolean invokeMethod(String methodName, Map<String, Object> inputs, Map<String, Object> output, Map<String, Object> options) {
4
5        if (methodName == 'ProductInOrder') {
6            productInOrder(inputs, options, output);
7        }
8        return true;
9    }
10
11    //Argument1: ProductCode to be checked
12    //Output: return true if product exists in the given order else false
13    public void productInOrder(Map<String, Object> inputMap, Map <String, Object> options, Map<String, Object> outMap) {
14
15        List <Object> arguments = (List <Object>) inputMap.get('arguments');
16        Boolean result = false;
17        outMap.put('result', false);
18        if (arguments != null && arguments.size() == 1 && arguments[0] != null) {
19            String prodCode = (String)arguments[0];
20            System.debug ('PRODUCT CODE: ' + prodCode);
21
22            CpqCartDocument cartDocument = (CpqCartDocument)FlowStaticMap.flowMap.get('cartDocument');
23            // update result variable based on your conditions
24
25            outMap.put('result', result);
26        }
27        System.debug('result ---- ' + JSON.serialize(outMap));
28    }
29}

Create a Function-Based Context Rule( Advanced Context Rule) 

To add the function definitions in custom metadata, perform the following.

  1. Go to Setup.

  2. In the Quick Find box, enter custom.

  3. Click Custom Metadata Types.

  4. Click Function Definition.

  5. Click Manage Function Definitions.

  6. Click New.

  7. Specify settings as follows:

    • Label: The function name displayed in the list on the Function Definitions tab.
    • Function Definition Name: The unique function name used by the API and managed packages, and which you use in formulas. The name must begin with a letter and use only alphanumeric characters and underscores. The name cannot end with an underscore or have two consecutive underscores.
    • ClassName: The Apex class containing the method that implements the logic for the function.
    • Method Name: The method that implements the logic for the function. Function Definition Detail
  • Create a function definition in Product Console.
    • Define function definition as shown below. Product In Order
    • Define function definition arguments as shown below. Function Argument
    • Create function based context rule as shown below. Context Function General
    • Create advanced context rules condition for above rule as shown below. Context Function Rule
    • Create ruleset and its children rule assignment as shown below. Context Function Test Context Children Rule

Context Rule Assignment 

  • Assign contextFunctionTest rule set to 500$ onetime charge of 200MBPS product as shown below MBPS

Run EPC JOB 

Generate EPC Compile data. This job initializes the cache data.
On the EPC Jobs page, click Start next to GENERATE COMPILE DATA. From the Price List dropdown list, select the price lists, and then click Apply

  • JOB

Context dimension mappings with functions must be converted to rule conditions with functions.

Note