# Create Cart from Basket

The Create Cart API provides the ability to create a cart after items have been added to a basket. The API provides the ability to convert the basket into a corresponding order and order line item.

```nolang
POST /v3/carts
```

First, offers are configured without a cart, then this API is called to add the basket contents to the cart (order and order line items) before submitting. The API creates a new cart. The API will validate and price the cart after adding the items and returns a link to the full cart contents.

Beginning with the Summer 2019 release, the CacheAPI.CreateCartFromContextKey configuration setting can be used to determine the behavior of the Create Cart API. If set to true, the Create Cart API uses the cartContextKey passed in the input to create the cart. If set to false or not specified, the JSON result from the Basket API must be passed in the request body of the Create Cart API to create the cart. The API requires the user account, catalog code, and cartItems in the body of the request.

You can create multiple carts with a single Create Cart API call. See [Create Cart from Basket](/docs/industries/cme/guide/comms-t-create-cart-from-basket.md) for an example.

You can batch process multiple carts at one time. See [Create Cart from Basket](/docs/industries/cme/guide/comms-t-create-cart-from-basket.md) for an example.

You can submit an order when the cart is created. See [Create Cart from Basket](/docs/industries/cme/guide/comms-t-create-cart-from-basket.md) for an example.

You can pass additional order capture information when creating an order from a basket. See [Create Cart from Basket](/docs/industries/cme/guide/comms-t-create-cart-from-basket.md) for an example.

## Rules Supported

- [Context Eligibility Rules for Pricing](/docs/industries/cme/guide/comms-t-context-eligibility-rules.md)

- Pricing Interface (see [pricing plans](https://help.salesforce.com/s/articleView?id=ind.comms_setting_up_attribute_based_pricing_using_pricing_plans.htm), such as [ABP](https://help.salesforce.com/s/articleView?id=ind.comms_attribute_based_pricing.htm))

- [Attribute-Based Pricing](https://help.salesforce.com/s/articleView?id=ind.comms_attribute_based_pricing.htm)

- For known users, product validation rules are applied.

## Prerequisites

- Run the [Load API Metadata](https://help.salesforce.com/s/articleView?id=ind.comms_load_api_metadata.htm) job (Vlocity CMT Administration > Cacheable API Jobs > Load API Metadata).

- Run all of the jobs under the Populate API Cache window.

- The [CacheAPIFields](https://help.salesforce.com/s/articleView?id=ind.comms_t_cpq_configuration_settings_reference\_148927.htm) custom setting must be set to true for versions prior to CME 103.1.14.

- See [Create Cart from Basket](/docs/industries/cme/guide/comms-t-create-cart-from-basket.md) for additional recommendations.

## API Parameters and Response Format

For API parameter names and descriptions, see [Digital Commerce REST API](https://developer.salesforce.com/docs/industries/cme/references/dc-rest1?meta=Summary).

## Example

First, create a basket. To create a basket, first run the Basket API. Then, use the output of the Basket API as input for the Cart API. Be sure to add the catalogCode and accountId parameters.

The body should resemble the following (note that JSONResult has been truncated for clarity):

```nolang
{"JSONResult" : "{\"totalSize..."productCategories\":{\"totalSize\":0}}]}}]}","catalogCode" : "catalog_code","accountId" : "account_ID"}
```

Now call the API:

```nolang
https://{salesforce_domain}/services/apexrest/vlocity_cmt/v3/carts
```

The response includes the order number and order ID.

## Batch Processing Example 1

You can create multiple carts with a single API call by specifying the multipleCartContext parameter in the header.

:::note
By default, pricing and basket validation are turned off when using the Create Cart API for bulk processing.
:::

```nolang
URL:
/services/apexrest/featurehvo/v3/carts?validate=false&price=false&validatebasket=false

Header:
{"multipleCartContext":[
  {"accountId": "0016g000005GhgS",
  "catalogCode": "Phones",
  "cartContextKey": "03e2574d420baabe40c2210970afc837",
  "orderItemFields": [
  {
    "bundleContextKey": "12b2d2fbbd91fab9b62c3689caf7e5a3",
    "lineItemKey": "12b2d2fbbd91fab9b62c3689caf7e5a3",
    "Description": "iphone 4"
  }
  ],"orderFields": {
    "Description": "iphone 4"}
  },
  {
    "accountId": "0016g00000LxUPf",
    "catalogCode": "Phones",
    "cartContextKey": "248c981213da8d58650d9c50ca578aff",
    "orderItemFields": [
  {
    "bundleContextKey": "fcec25cc2013eca9f73db00b35020c29",
    "lineItemKey": "fcec25cc2013eca9f73db00b35020c29",
    "Description": "iphone 5"}],
    "orderFields": {"Description": "iphone 5"}
  },
  {"accountId": "0016g00000LxUPL",
  "catalogCode": "Phones",
  "cartContextKey": "4ca6c995e66d71c163f509302566f7d6",
  "orderItemFields":
  [
    {"bundleContextKey": "d61e242102fb4329e8a6acf273316159",
    "lineItemKey": "d61e242102fb4329e8a6acf273316159",
    "Description": "iphone 5s"}],
    "orderFields": {
      "Description": "iphone 5s"
    }
  }
]
}
```

Output:

![Batch Processing output](https://a.sfdcstatic.com/developer-website/sfdocs/industries/media/creat4ecart_ex1.png)

## Batch Processing Example 2

You can batch process multiple carts at one time.

```nolang
URL:
/services/apexrest/featurehvo/v3/carts?validate=false&price=false&validatebasket=false

Header
{"multipleCartContext":[
{"accountId": "0016g000005GhgS",
"catalogCode": "Phones",
"cartContextKey": "03e2574d420baabe40c2210970afc837",
"orderItemFields": [
{"bundleContextKey": "12b2d2fbbd91fab9b62c3689caf7e5a3",
"lineItemKey": "12b2d2fbbd91fab9b62c3689caf7e5a3",
"Description": "iphone 4"
}
],
"orderFields": {
"Description": "iphone 4"
}
},
{"accountId": "0016g00000LxUPf",
"catalogCode": "Phones",
"cartContextKey": "248c981213da8d58650d9c50ca578aff",
"orderItemFields": [
{
"bundleContextKey": "fcec25cc2013eca9f73db00b35020c29",
"lineItemKey": "fcec25cc2013eca9f73db00b35020c29",
"Description": "iphone 5"
}
],
"orderFields": {
"Description": "iphone 5"
}
},
{
"accountId": "0016g00000LxUPL",
"catalogCode": "Phones",
"cartContextKey": "4ca6c995e66d71c163f509302566f7d6",
"orderItemFields": [
{
"bundleContextKey": "d61e242102fb4329e8a6acf273316159",
"lineItemKey": "d61e242102fb4329e8a6acf273316159",
"Description": "iphone 5s"
}
],
"orderFields": {
"Description": "iphone 5s"}
},
{"accountId": "0016g00000LxUPa",
"catalogCode": "Phones",
"cartContextKey": "2a060ccffb639df16448388fb944937c",
"orderItemFields": [
{
"bundleContextKey": "3d2e9657bb10e96ef663669e5cd9f8e2",
"lineItemKey": "3d2e9657bb10e96ef663669e5cd9f8e2",
"Description": "iphone 6"}],
"orderFields": {
"Description": "iphone 6"
}
},
{
"accountId": "0016g00000LyRtK",
"catalogCode": "Phones",
"cartContextKey": "6eadc50ae768c46cb5a24847918cd84e",
"orderItemFields": [
{
"bundleContextKey": "90801bc3d3ca1f1211fd7eff9fc576d9",
"lineItemKey": "90801bc3d3ca1f1211fd7eff9fc576d9",
"Description": "iphone 7"
}
],"orderFields": {
"Description": "iphone 7"
}
}
]
}
```

Output:

![Batch Processing output](https://a.sfdcstatic.com/developer-website/sfdocs/industries/media/createcart_ex2.png)

## Example to Create Cart and Submit Order

You can submit an order when the cart is created.

:::note
Creating assets using the CreateCart API can only be used with a single Create Cart API call.
:::

```nolang
URL:
/services/apexrest/featurehvo/v3/carts?createAsset=true

Header:
{
   "accountId": "0014R00002k5vPG",
   "catalogCode": "Phones",
   "cartContextKey": "1b42e612a401a072324c7ca026bc4e2f"
}
```

Output:

![Create Cart and Submit Order output](https://a.sfdcstatic.com/developer-website/sfdocs/industries/media/createcart_ex3.png)

## Example Showing Passing Additional Parameters

You can pass additional order capture information when creating an order from a basket. After a Basket has been created, and the user is ready to check out, you can pass additional information in the order header and order Line Items. For example, you might want to pass an updated billing address or an external Order Management system ID.

```nolang
URL:
/services/apexrest/featurehvo/v3/carts

Header:
{
   "accountId": "0016g000005GhgS",
   "catalogCode": "Phones",
   "cartContextKey": "03e2574d420baabe40c2210970afc837",
   "orderItemFields": [
       {
           "bundleContextKey": "a561caa70bd9e2343a3066ed316bdec8",
           "lineItemKey": "a561caa70bd9e2343a3066ed316bdec8",
           "Description": "new iphone 4",
           "featurehvo__ItemName__c": "iphone4"
       }
     ],
"orderFields": {
    "Description": "Apple Products",
    "featurehvo__Email__c": "johndoe@gmail.com"
   }
}
```

Output:

![Example Showing Passing Additional Parameters](https://a.sfdcstatic.com/developer-website/sfdocs/industries/media/createcart_ex4.png)

To prevent inadvertently updating a field that may invalidate ancillary operations, by default, the Create Cart API is restricted to updating only the following Order and OrderItem fields:

Order Item:

- Line Description - Description

- Item Name - vlocity_cmt**ItemName**c

- Billing Account - vlocity_cmt**BillingAccountId**c

- Service Account - vlocity_cmt**ServiceAccountId**c

- Service Point - vlocity_cmt**ServicePointId**c

Order:

- Description - Description

- Order Name - Name

- Order Reference Number - OrderReferenceNumber

- Order Type - Type

- Delivery Installation Status - vlocity_cmt**Delivery_Installation_Status**c

- Email - vlocity_cmt**Email**c

- Lead Source - vlocity_cmt**LeadSource**c

- Billing Account - vlocity_cmt**DefaultBillingAccountId**c

- Service Account - vlocity_cmt**DefaultServiceAccountId**c

- Service Point - vlocity_cmt**DefaultServicePointId**c

To update any fields other than the fields in the preceding list, you can create a fieldset on Orders for Order field updates and OrderProducts for OrderItem field updates. The fieldset name and label should match CartAllowedFieldSet for both Orders and OrderProducts.

By default, the Create Cart API is restricted to updating only the following Quote and QuoteItem fields:

Quote:

- Description: Description
- Quote Name: Name
- Lead Source: vlocity_cmt**LeadSource**c
- Billing Account: vlocity_cmt**DefaultBillingAccountId**c
- Service Account: vlocity_cmt**DefaultServiceAccountId**c
- Service Point: vlocity_cmt**DefaultServicePointId**c

QuoteItem:

- Description: Description
- QuoteItem Name: ItemName\_\_c
- Billing Account: vlocity_cmt**BillingAccountId**c
- Service Account: vlocity_cmt**ServiceAccountId**c
- Service Point: vlocity_cmt**ServicePointId**c

## Custom Settings and Query Parameter Recommendations

The following settings are recommended to increase performance when using the Create Cart API. These settings are appropriate when creating either single or multiple carts.

| Setting                             | Type                    | Value                     |
| ----------------------------------- | ----------------------- | ------------------------- |
| CachedQueryMode                     | CPQ Configuration Setup | True                      |
| CacheEnabled                        | CPQ Configuration Setup | True                      |
| checkContextAuthenticity            | query parameter         | false                     |
| contextKey                          | query parameter         | contextKey from getOffers |
| price                               | query parameter         | false                     |
| returnOrderNumber                   | query parameter         | false                     |
| UseAssetReferenceIdForParentAndRoot | CPQ Configuration Setup | True                      |
| validate                            | query parameter         | false                     |
| validatebasket                      | query parameter         | false                     |

**See Also**

- [About Field Sets](https://help.salesforce.com/articleView?id=sf.fields_about_field_sets.htm)
