---
schema_version: "1"
id: d856ec35c541cb398af91d175f9d6c71ae7d5204dd7fcb78d5737d8c6a832e2b
title: Custom Flow for Pricing Variable Calculation
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/industries/cme/guides/comms-custom-flow-pricing-variable.md
canonical_url: https://developer.salesforce.com/docs/industries/cme/guide/comms-custom-flow-pricing-variable.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 8f7ad091dd03d4fe092b8ae1df1727c73a592066103b0ac62b921e5db521dd70
products:
  - Communications, Media, Energy and Utilities APIs (Managed Package)
  - Industries Developer Documentation
---
# 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](https://developer.salesforce.com/docs/media/custom_apex_class.png "Custom 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](https://developer.salesforce.com/docs/media/create_interface.png "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](https://developer.salesforce.com/docs/media/input_map.png "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](https://developer.salesforce.com/docs/industries/cme/guides/use-cart-document-cpq.md).
- 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](https://developer.salesforce.com/docs/media/output_map.png "Output Map")

## Sample Code:

```
private void calculatePostInvoke(Map<String, Object> input,Map<String, Object> output)
{
    System.debug('debug post invoke');
    Map<String, Object> pricingVariableCodeToValueMap = (Map<String,Object>)input.get('pricingVariableCodeValueMap');
    System.debug('post invoke price data: '+json.serialize(pricingVariableCodeToValueMap));
    //Getting the cart document instance
    vlocity_digital.CpqCartDocument cart = (vlocity_digital.CpqCartDocument)input.get('cartDocument');
    //method to get all the line items
    Map<String, Object> outputMap = cart.call('getAllItems', new Map<String, Object>{'hierarchyLevel'=>-1});
    Map<String, vlocity_digital.CpqCartDocumentItem> allItemsMap = (Map<String, vlocity_digital.CpqCartDocumentItem>) outputMap.get('result');
    String currentItemId = (String)input.get('itemId');
    System.debug('debug currentItemId:'+currentItemId);
    //Getting the cartDocumentItem having id as currentItemId
    vlocity_digital.CpqCartDocumentItem currentItem = allItemsMap.get(currentItemId);
    System.debug('debug post output: '+json.serialize(output));
    //getting the line item fields
    Map<String, Object> testOTMap = currentItem.call('getItemField', new Map<String, Object>{'field'=>'vlocity_digital__OneTimeCharge__c'});
    System.debug('debug post otc from cartdoc : '+json.serialize(testOTMap));
}
```

- 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](https://developer.salesforce.com/docs/media/relational_fields.png "Releational Fields")

- Query to get the GUID status of the org:

```
SELECT 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.

```
GET: /services/apexrest/vlocity_digital/v2/cpq/carts/<cartId>/items?fields=OLI_customfield
```
