---
schema_version: "1"
id: 776e5418cf59c26664f98fc4d3e6cad2ec6d9a806dce083f1003b1f435db5c54
title: Use Cart Documents
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/industries/cme/guides/comms-use-cart-documents.md
canonical_url: https://developer.salesforce.com/docs/industries/cme/guide/comms-use-cart-documents.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 4007c0bb469901a6f99b07a1fcb9b6ef908572410ba7eae56a47ded25f99f4ee
products:
  - Communications, Media, Energy and Utilities APIs (Managed Package)
  - Industries Developer Documentation
---
# 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 Name          | Arguments                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   | Description                            |
    | -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------- |
    | `getCartField`       | `field`                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     | Method to get the fields of the cart   |
    | `setCartField`       | `` field`, `value ``                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        | Method to set the cart field value     |
    | `getItemIds`         | NA                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          | Method to get the item IDs             |
    | `getAllItems`        | ``hierarchyLevel`(optional: -1 full hierachy)` | Method to get all the line items node                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
    | `getChildItems`      | `nsp` `+` `'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       |
    | `clearExternalPrice` | NA                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          | Method 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 Name         | Arguments                                                 | Description                                                                                                               |
    | ------------------- | --------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------- |
    | `getItemField`      | `field`                                                   | Method to get an item field                                                                                               |
    | `setItemField`      | `` field`, `value ``                                      | Method to set the value of an item field                                                                                  |
    | `getItemFields`     | `fields`                                                  | Method to get multiple item fields                                                                                        |
    | `setItemFields`     | `fieldValueMap`                                           | Method to set multiple item fields                                                                                        |
    | `getItemAttributes` | `` flattenedAttributeValues`, `useDisplayTextForValues `` | Method to get the attribute node of the item. There are 2 parameters according to which the attribute nodes are returned. |
    | `setItemAttributes` | `attributeCodeToValueMap`                                 | Method to see the item attributes                                                                                         |
    | `getChildItemIds`   | NA                                                        | Method to get the child item IDs                                                                                          |

## Sample Code

```JSON
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());
        }
    }
}
```
