A common question that I get while answering community questions about Salesforce B2B Commerce Cloud is "How do I map my custom fields / objects associated with the cart record over to the order summary records?". This is a fairly common question because almost every commerce site has a bit of customization, after all, that's the power of the Salesforce platform!
We'll start with the first concept, mapping custom fields on the existing objects onto the order summary. In a upcoming post, we'll cover the concept of mapping custom objects once an order is placed.
Mapping Cart Fields to Order Fields in Salesforce B2B Commerce Cloud
Let's start by setting up an example that we'll follow through the lifecycle.
Scenario:
Imagine you're working on building a commerce storefront and you identify there is a requirement that you need to order the carts items when you show the cart to the customer.
![Cart Page Sort Order](https://static.wixstatic.com/media/fbcdbf_784900dd774d46809fed5c204439a831~mv2.png/v1/fill/w_980,h_402,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/fbcdbf_784900dd774d46809fed5c204439a831~mv2.png)
This is pretty common in manufacturing and other highly technical industries, customers want to be able to manually sort their carts and this sort order needs to be retained and also maintained throughout the order when it's submitted into ERP.
How would you accomplish this?
Well right out of the gate we can identify a few things that are important:
This isn't something that can be accomplished out of the box
We likely will need at least one custom field on the cart item in order to save the sort the customer decides on
We want to make sure this sort order makes it way to order as well when the order is submitted into ERP
We are going to need to update a few components in order to visually show
Luckily for us, there is some native functionality that will help us solve #2 and #3! This is where the standard "Map Cart Data to Order Data" comes in. This feature is responsible for mapping custom fields between "common" objects, and supported data types.
For a full read through of the raw documentation, you can navigate here https://developer.salesforce.com/docs/atlas.en-us.b2b_b2c_comm_dev.meta/b2b_b2c_comm_dev/b2b_b2c_comm_import_export_carttoorder.htm
There are four conditions that the field must meet in order to be synced over at a high level:
The field must be a supported data type (the full list is included in the documentation link above, but a key note is that lookup fields are not supported)
There must be an exactly matching field on the downstream corresponding objects (more on this in a bit)
The feature must be enabled (this is true by default, unless you've manually turned it off)
The field must map through all necessary objects (more on this in a moment)
Mapped Objects
To provide a bit of a clearer picture then the documentation does, the following table is how the tables will map between a Cart, Order, and Order Summary record.
Even though you may not use the Order objects, it is required to have a matching field on that columns respective object for the field to be correctly mapped to the Order Summary objects.
![Cart to Order Sync](https://static.wixstatic.com/media/fbcdbf_6bb279dcb5ef48d296684de02538ec94~mv2.jpeg/v1/fill/w_860,h_365,al_c,q_80,enc_auto/fbcdbf_6bb279dcb5ef48d296684de02538ec94~mv2.jpeg)
Cart | Order | Order Summary |
---|---|---|
Cart | Order | Order Summary |
Cart Item | Order Item | Order Product Summary |
Cart Item Price Adjustment | Order Item Adjustment Line Item | Order Product Adjustment Line Summary |
Cart Tax | Order Item Tax Line Item | Order Product Tax Line Item Summary |
Cart Delivery Group | Order Delivery Group | Order Delivery Group Summary |
Web Cart Adjustment Group | Order Adjustment Group | Order Adjustment Group Summary |
I've seen many people create a field on the Cart and Order Summary objects that match #1 - #3 criteria mentioned above, but they forget to create the same object on the Order objects which causes the sync not to work.
Put in a visual example, here is the Sort Order example we're about to go over:
![Visual of the mapping objects](https://static.wixstatic.com/media/fbcdbf_b689ae5e82a5481db89f93471d75e502~mv2.jpeg/v1/fill/w_980,h_270,al_c,q_80,usm_0.66_1.00_0.01,enc_auto/fbcdbf_b689ae5e82a5481db89f93471d75e502~mv2.jpeg)
![Visual of the mapping object working.](https://static.wixstatic.com/media/fbcdbf_2604aa8ee3054f4385f52e025cac6522~mv2.jpeg/v1/fill/w_980,h_262,al_c,q_80,usm_0.66_1.00_0.01,enc_auto/fbcdbf_2604aa8ee3054f4385f52e025cac6522~mv2.jpeg)
Without the field on the Order Item as well, the mapping will not correctly bring the values onto the Order Product Summary record.
Walkthrough of the Scenario
Now that we have the foundation understood, let's go through our scenario and set it up. What we're going to need is a new number field to store this sort order information. We'd like this to be called 'Sort Order' and have that field be a data type of 'Number'. Again, we want to capture that information when the user is visually changing the sorting in the Cart Page, but we want to make sure that sort order is kept when the order summary is created.
Step 1 - Create the Cart Item Field
Navigate to Setup > Objects > Cart Item.
Create a new fields called 'Sort Order' of type 'Number'. The field should look like this when you're done:
![Cart Item Field](https://static.wixstatic.com/media/fbcdbf_0fb7d0cdf5da439cb980453614dba08b~mv2.png/v1/fill/w_980,h_345,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/fbcdbf_0fb7d0cdf5da439cb980453614dba08b~mv2.png)
Step 2 - Create the Order Item Field
Navigate to Setup > Objects > Cart Item.
Create a new fields called 'Sort Order' of type 'Number'. The field should look like this when you're done:
![Order Product Field](https://static.wixstatic.com/media/fbcdbf_8f2e30884d2e45509f4a56a76e7c8e94~mv2.png/v1/fill/w_980,h_342,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/fbcdbf_8f2e30884d2e45509f4a56a76e7c8e94~mv2.png)
Step 3 - Create the Order Product Summary Field
Navigate to Setup > Objects > Cart Item.
Create a new fields called 'Sort Order' of type 'Number'. The field should look like this when you're done:
![Order Product Summary Field](https://static.wixstatic.com/media/fbcdbf_9594be04c46f4945adf458ca4d11a2a4~mv2.png/v1/fill/w_980,h_335,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/fbcdbf_9594be04c46f4945adf458ca4d11a2a4~mv2.png)
Step 4 - Test the mapping
Now that we have all those fields created, we're going to want to generate a new cart. You can do this by logging in as a test user and adding an item to your cart.
If you don't already have a storefront setup, go ahead and navigate over to my getting started video
For this test, you'll need to update the Sort Order values manually since we don't have a custom UI to manage that for us in the experience. Updating the cart item can be accomplished many ways, but once you've done that you should see the values show up on the Cart Items. (I've added the field in this screenshot below to my Cart Items related list to make it easier to see these values)
![Cart Item Sort Order](https://static.wixstatic.com/media/fbcdbf_4c8271e5fd1b45b78b0f58db1a4e4e19~mv2.png/v1/fill/w_980,h_441,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/fbcdbf_4c8271e5fd1b45b78b0f58db1a4e4e19~mv2.png)
Now place your order in the storefront! This will take your cart, and create a order & order summary record. If you've followed the above steps correctly, you'll find that you now have an order summary with the custom field on the order product summary records!
![Order Product Summary Sort Order](https://static.wixstatic.com/media/fbcdbf_2df8881cdedb4a34a55f18964b033c6a~mv2.png/v1/fill/w_980,h_492,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/fbcdbf_2df8881cdedb4a34a55f18964b033c6a~mv2.png)
If you're looking for more information on how to turn off this functionality you can find that by looking at this documentation article: https://developer.salesforce.com/docs/commerce/salesforce-commerce/guide/b2b-b2c-comm-disable-cart-to-order-mappings-for-custom-fields.html
Congrats! You've now successfully mapped a custom fields across the cart to order life cycle! In one of the coming blogs, we'll cover custom object mapping. This is slightly more difficult because there is not a native service to assist in that mapping process. However, it's a fairly common practice to have a few custom objects associated to your cart process.