Custom Flow for Pricing Variable Calculation

You can implement custom flow for pricing variable calculation with Standard Cart APIs. The process involves 2 steps:

  1. Setting Up the Interface
  2. Using the Existing Data

Setting Up the Interface 

  1. Create a custom apex class as follows: Custome Apex Class Ensure to add the namespace of the org before class and method names in the managed orgs.

  2. Create an interface as follows: Create an Interface

  3. Run EPC Compile job.

Using the Existing data 

The data passed in the input and output map of the calculate method is as follows:

Input Map: 

  • String objectType: This field stores the type of object passed like orderItem. It can be fetched using get operator.
  • Boolean isRoot: This value represents if the item being calculated is the bundle’s root. It can be fetched using get operator.
  • Double effectiveQuantity: This field stores the quantity of the item passed. It can be fetched using get operator.
  • Map<String, Object> pricingVariableCodeValueMap: This map stores values of relevant pricing variables mapped to its code. It can be fetched using get operator.

Input Map

  • Map<String, Integer> pricingVariableCodeToRoundingScale: This has the map for rounding scale to pricing variable code. It can be fetched using get operator.
  • Map<String, String> pricingVariableCodeToFieldBindingLabels: This has the map for field binding labels to pricing variable code. It can be fetched using get operator.
  • String isCostAndMarginFeatureEnabled: This value represents if Cost and Margin feature is enabled. It can be fetched using get operator.
  • String marginCalculationType: This field stores margin calculation type which is information relevant to cost and margin feature. It can be fetched using get operator.
  • CartDocument cartDocument: This is a custom-type map that stores extensive information about the current cart. This cannot be extracted directly using get operator. Please use methods specifically designed for CartDocument to get relevant information. For more information, see Use Cart Documents.
  • String itemId: This stores the itemId for the current lineitem that is being processed. Depending on the org GUID (UseAssetReferenceIdForParentAndRoot CPQ Config) value, it can be salesforceId or assetreferenceId. It can be fetched using get operator.

NOTE:

Please do not try using debug statements that serialize the input map.

System.debug('debug post input: '+json.serialize(input));

Input map contains an object for CartDocument, which is a custom Java type and cannot be serialized directly by the apex layer. Please use methods specifically designed for CartDocument to get relevant information.

Output Map: 

  • Map<String, Object> calculatedPricingLogData: This stores the pricing information for pricing variables of the processed item. It can be fetched using get operator.

Output Map

Sample Code: 

1private void calculatePostInvoke(Map<String, Object> input,Map<String, Object> output)
2{
3    System.debug('debug post invoke');
4    Map<String, Object> pricingVariableCodeToValueMap = (Map<String,Object>)input.get('pricingVariableCodeValueMap');
5    System.debug('post invoke price data: '+json.serialize(pricingVariableCodeToValueMap));
6    //Getting the cart document instance
7    vlocity_digital.CpqCartDocument cart = (vlocity_digital.CpqCartDocument)input.get('cartDocument');
8    //method to get all the line items
9    Map<String, Object> outputMap = cart.call('getAllItems', new Map<String, Object>{'hierarchyLevel'=>-1});
10    Map<String, vlocity_digital.CpqCartDocumentItem> allItemsMap = (Map<String, vlocity_digital.CpqCartDocumentItem>) outputMap.get('result');
11    String currentItemId = (String)input.get('itemId');
12    System.debug('debug currentItemId:'+currentItemId);
13    //Getting the cartDocumentItem having id as currentItemId
14    vlocity_digital.CpqCartDocumentItem currentItem = allItemsMap.get(currentItemId);
15    System.debug('debug post output: '+json.serialize(output));
16    //getting the line item fields
17    Map<String, Object> testOTMap = currentItem.call('getItemField', new Map<String, Object>{'field'=>'vlocity_digital__OneTimeCharge__c'});
18    System.debug('debug post otc from cartdoc : '+json.serialize(testOTMap));
19}
  • Based on the itemId (salesforceId or GUID) in the input map, additional queries can be written to find out more details about the item including relational fields.

Fields

  • Query to get the GUID status of the org:
1SELECT Name, vlocity_digital__SetupValue__c FROM vlocity_digital__CpqConfigurationSetup__c WHERE Name = 'UseAssetReferenceIdForParentAndRoot'
  • To get the XLI custom field values without doing an additional SOQL call, we can use the ‘fields’ param in the getCartItems API. This would populate this custom field response in cartDocument in case it is a non-null value.
1GET: /services/apexrest/vlocity_digital/v2/cpq/carts/<cartId>/items?fields=OLI_customfield