Cart documents are trimmed and flexible JSON structures that you can use in the CPQ on Core services. You can then use the output JSON to send responses to upper layer APIs such as the Digital Commerce APIs.
To use a cart document:
-
Identify the instance of the cart document from the input map.
-
Call the wrapper APIs using the format:
CpqCartDocument.call(methodName, args)
The results of all of these methods are in JSON format, with a single key-value pair: "result": value. Set methods (setCartField, setExternalprice) require arguments without “result” node.Method Name Arguments Description getCartFieldfieldMethod to get the fields of the cart setCartFieldfield`, `valueMethod to set the cart field value getItemIdsNA Method to get the item IDs getAllItems``hierarchyLevel (optional: -1 full hierachy)Method to get all the line items node getChildItemsnsp+'AssetReferenceId__c'Method to get all the child line items setExternalPrice{"itemFieldsInfo":{"58f1a32b-a0f9-4455-9e53-dcd4dd839bb0":{"vlocity_digital__OneTimeCharge__c":{"value":100,"detail":"Priced from AttributePricingProcedure","source":"ABP","code":"OT_STD_PRC"},"vlocity_digital__RecurringCharge__c":{"value":50,"detail":"Priced from AttributePricingProcedure","source":"ABP","code":"REC_MNTH_STD_PRC"}},"57d29a96-59c0-49b9-b4f8-a61f5d51a7ef":{"vlocity_digital__OneTimeCharge__c":{"value":70,"detail":"Priced from AttributePricingProcedure","source":"ABP","code":"OT_STD_PRC"},"vlocity_digital__RecurringCharge__c":{"value":80,"detail":"Priced from AttributePricingProcedure","source":"ABP","code":"REC_MNTH_STD_PRC"}}},"timePlanPolicyList":[{"ID":"57d29a96-59c0-49b9-b4f8-a61f5d51a7ef","TimePolicyName":"Purchase Date To End Of Plan Duration","TimePlanName":"12 Months Time Plan"},{"ID":"58f1a32b-a0f9-4455-9e53-dcd4dd839bb0","TimePolicyName":"Purchase Date To End Of Plan Duration","TimePlanName":"6 Months Time Plan"}]}Method to set the external price clearExternalPriceNA Method to clear the external price -
If you've extended or modified the pricing plan helper in your org, then update the existing class based on the new version of the pricing plan helper.
-
Access the line items in the basket using the CpqCartDocumentItem wrapper class.
Use these methods to get and set fields on line items. The results of all of these methods are in JSON format, with a single key-value pair: "result": value. Set methods (setItemField, setItemFields, setItemAttributes) require arguments without “result” node.
Method Name Arguments Description getItemFieldfieldMethod to get an item field setItemFieldfield`, `valueMethod to set the value of an item field getItemFieldsfieldsMethod to get multiple item fields setItemFieldsfieldValueMapMethod to set multiple item fields getItemAttributesflattenedAttributeValues`, `useDisplayTextForValuesMethod to get the attribute node of the item. There are 2 parameters according to which the attribute nodes are returned. setItemAttributesattributeCodeToValueMapMethod to see the item attributes getChildItemIdsNA Method to get the child item IDs
Sample Code
global with inherited sharing class CustomPricingPlanMethod implements vlocity_cmt.VlocityOpenInterface {
global void invokeMethod(Map<String, Object> input, Map<String, Object> output, Map<String, Object> options){
try
{
// Getting the cart document instance
vlocity_cmt.CpqCartDocument cart = (vlocity_cmt.CpqCartDocument) input.get('cartDocument');
// method to get all the line items
Map<String, Object> output = cart.call('getAllItems', new Map<String, Object>{'hierarchyLevel'=>-1});
Map<String, vlocity_cmt.CpqCartDocumentItem> allItemsMap = (Map<String, vlocity_cmt.CpqCartDocumentItem>) output.get('result');
// getting the cartDocumentItem having line number as 0001
vlocity_cmt.CpqCartDocumentItem rootItem = allItemsMap.get('0001');
// getting the line item fields
output = rootItem.call('getItemField', new Map<String, Object>{'field'=>'vlocity_cmt__OneTimeCharge__c'});
Double otc = (Double) output.get('result') + 10;
// setting the line item field
output = rootItem.call('setItemField', new Map<String, Object>{'field'=>'vlocity_cmt__OneTimeCharge__c', 'value'=> otc });
// setting multiple line item field
output = rootItem.call('setItemFields', new Map<String, Object>{'fieldValueMap'=>new Map<String, Object>{'vlocity_cmt__OneTimeCharge__c'=>50, 'vlocity_lwc22__OneTimeTotal__c'=>50}} );
// getting a particular item field
output = rootItem.call('getItemFields', new Map<String, Object>{'fields'=> new Set<String>{'vlocity_cmt__OneTimeCharge__c', 'vlocity_lwc22__OneTimeTotal__c'}});
// setting a cart field
output = cart.call('setCartField', new Map<String, Object> { 'field'=> 'vlocity_cmt__EffectiveOneTimeTotal__c', 'value'=>otc } );
// getting the line item attributes and creating a map
Map<String, Object> categoryToAttrCodeValueMap = (Map<String, Object>) rootItem.call('getItemAttributes', null).get('result');
// sample code to put attribute values
Map<String, Object> attrCodeValueMap = (Map<String, Object>) categoryToAttrCodeValueMap.get('MLS');
Double codeMLSValue = (Double) attrCodeValueMap.get('CodeMLS');
if(codeMLSValue==20)
codeMLSValue = 10;
else
codeMLSValue = 20;
attrCodeValueMap.put('CodeMLS', codeMLSValue);
// setting the item attributes
output = rootItem.call('setItemAttributes', new Map<String, Object>{'attributeCodeToValueMap'=> categoryToAttrCodeValueMap});
// getting the line number
String rootLineNumber = (String) rootItem.call('setItemField', new Map<String, Object>{'field' =>'vlocity_cmt__LineNumber__c'}).get('result');
// getting the child line items
vlocity_cmt.CpqCartDocumentItem childItem = (vlocity_cmt.CpqCartDocumentItem) ((Map<String, Object>)cart.call('getChildItems', new Map<String, Object>{'vlocity_cmt__AssetReferenceId__c'=>rootLineNumber}).get('result')).values()[0];
output = childItem.call('getItemField', new Map<String, Object>{'field'=>'vlocity_cmt__OneTimeCharge__c'});
String childLineNumber = (String) childItem.call('getItemField', new Map<String, Object>{'field'=>'vlocity_cmt__LineNumber__c'}).get('result');
// clear external price
cartDocument.call('clearExternalPrice', null);
//method to get sobject id of all the affected line items for 1b
Map<String, Object> outputZ = cart.call('getAffectedItems', new Map<String, Object>{'hierarchyLevel'=>-1});
//method to get all the affected line items for 1b
Map<String, Object> affectedItemIds = cart.call('getAffectedItemIds', null);
}
catch(Exception ex)
{
System.debug('message: ' + ex.getMessage() + '\n' + ex.getStackTraceString());
}
}
}