Use Cart Documents

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:

  1. Identify the instance of the cart document from the input map.

  2. 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 NameArgumentsDescription
    getCartFieldfieldMethod to get the fields of the cart
    setCartFieldfield`, `valueMethod to set the cart field value
    getItemIdsNAMethod 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
    clearExternalPriceNAMethod to clear the external price
  3. 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.

  4. 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 NameArgumentsDescription
    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
    getChildItemIdsNAMethod to get the child item IDs

Sample Code 

1global with inherited sharing class CustomPricingPlanMethod implements vlocity_cmt.VlocityOpenInterface {
2
3    global void invokeMethod(Map<String, Object> input, Map<String, Object> output, Map<String, Object> options){
4
5        try
6        {
7
8            // Getting the cart document instance
9            vlocity_cmt.CpqCartDocument cart = (vlocity_cmt.CpqCartDocument) input.get('cartDocument');
10
11            // method to get all the line items
12            Map<String, Object> output = cart.call('getAllItems', new Map<String, Object>{'hierarchyLevel'=>-1});
13
14            Map<String, vlocity_cmt.CpqCartDocumentItem> allItemsMap = (Map<String, vlocity_cmt.CpqCartDocumentItem>) output.get('result');
15
16            // getting the cartDocumentItem having line number as 0001
17            vlocity_cmt.CpqCartDocumentItem rootItem = allItemsMap.get('0001');
18
19            // getting the line item fields
20            output = rootItem.call('getItemField', new Map<String, Object>{'field'=>'vlocity_cmt__OneTimeCharge__c'});
21
22            Double otc = (Double) output.get('result') + 10;
23
24            // setting the line item field
25            output = rootItem.call('setItemField', new Map<String, Object>{'field'=>'vlocity_cmt__OneTimeCharge__c', 'value'=> otc });
26
27            // setting multiple line item field
28            output = rootItem.call('setItemFields', new Map<String, Object>{'fieldValueMap'=>new Map<String, Object>{'vlocity_cmt__OneTimeCharge__c'=>50, 'vlocity_lwc22__OneTimeTotal__c'=>50}} );
29
30            // getting a particular item field
31            output = rootItem.call('getItemFields', new Map<String, Object>{'fields'=> new Set<String>{'vlocity_cmt__OneTimeCharge__c', 'vlocity_lwc22__OneTimeTotal__c'}});
32
33            // setting a cart field
34            output = cart.call('setCartField', new Map<String, Object> { 'field'=> 'vlocity_cmt__EffectiveOneTimeTotal__c', 'value'=>otc } );
35
36            // getting the line item attributes and creating a map
37            Map<String, Object> categoryToAttrCodeValueMap = (Map<String, Object>) rootItem.call('getItemAttributes', null).get('result');
38
39            // sample code to put attribute values
40            Map<String, Object> attrCodeValueMap = (Map<String, Object>) categoryToAttrCodeValueMap.get('MLS');
41            Double codeMLSValue = (Double) attrCodeValueMap.get('CodeMLS');
42
43            if(codeMLSValue==20)
44                codeMLSValue = 10;
45            else
46                codeMLSValue = 20;
47
48            attrCodeValueMap.put('CodeMLS', codeMLSValue);
49
50            // setting the item attributes
51            output = rootItem.call('setItemAttributes', new Map<String, Object>{'attributeCodeToValueMap'=> categoryToAttrCodeValueMap});
52
53            // getting the line number
54            String rootLineNumber = (String) rootItem.call('setItemField', new Map<String, Object>{'field' =>'vlocity_cmt__LineNumber__c'}).get('result');
55
56            // getting the child line items
57            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];
58
59            output = childItem.call('getItemField', new Map<String, Object>{'field'=>'vlocity_cmt__OneTimeCharge__c'});
60
61            String childLineNumber = (String) childItem.call('getItemField', new Map<String, Object>{'field'=>'vlocity_cmt__LineNumber__c'}).get('result');
62
63            // clear external price
64            cartDocument.call('clearExternalPrice', null);
65
66            //method to get sobject id of all the affected line items for 1b
67            Map<String, Object> outputZ = cart.call('getAffectedItems', new Map<String, Object>{'hierarchyLevel'=>-1});
68
69            //method to get all the affected line items for 1b
70            Map<String, Object> affectedItemIds = cart.call('getAffectedItemIds', null);
71
72        }
73        catch(Exception ex)
74        {
75            System.debug('message: ' + ex.getMessage() + '\n' + ex.getStackTraceString());
76        }
77    }
78}