You are here:
Example: Calculate Margin by Using a Standard Cost Book
Salesforce Pricing doesn't bring cost data into the quote line or calculate margin automatically. This example shows one way to surface cost on the quote line item and calculate margin from it, using standard Salesforce Pricing building blocks and custom fields.
Required Editions
This article is an illustrative example, not a packaged feature or a supported configuration path. Object names, field names, tag names, and formulas in this topic are examples only. Your context definition, mapping, pricing recipe, and pricing procedure can be structured differently. Treat every name and value here as a placeholder, and adapt each step to match your Salesforce org. Work with a Salesforce administrator or implementation partner if you aren't familiar with context definitions, decision tables, and pricing procedures.
| Available in: Lightning Experience |
| Available in: Enterprise, Unlimited, and Developer Editions of Revenue Management (formerly Revenue Cloud) with the Revenue Cloud Growth license or the Revenue Cloud Advanced license. |
| User Permissions Needed | |
|---|---|
| To create and manage cost books: | Salesforce Pricing Design Time User |
In this example, a sales representative opens a quote, adds a product, and views three line-item values that Salesforce Pricing doesn't display by default: product cost, margin percentage, and margin amount. To achieve this outcome:
- Record the product cost in a cost book.
- Add custom fields to hold cost and margin on the quote and quote line item.
- Extend the context definition so the cost book ID and unit cost flow through pricing calculations.
- Build a decision table that looks up the cost for a product from its cost book entry.
- Add that decision table to a pricing recipe and pricing procedure so the engine fetches the cost during calculation.
- Display the new fields on the quote record layout.
Create a Cost Book and Cost Book Entry
Follow Define Cost Books to create a cost book and add a cost book entry for a product.
Add Custom Fields
The following field names are examples. Use names that fit your org’s naming conventions, and check for existing fields before creating new ones.
- On the Quote object, create a custom Text field to hold the related cost book ID (for example, `Example_Cost_Book_Id__c`).
-
On the Quote Line Item object, create:
- A Currency field to hold the unit cost (for example, `Example_Unit_Cost__c`).
- A Formula field for total cost (for example, `Example_Total_Cost__c` = `Example_Unit_Cost__c * Quantity`).
- A Formula field for margin percentage (for example, `Example_Margin_Percent__c` = `(NetTotalPrice - (Example_Unit_Cost__c * Quantity)) / NetTotalPrice`).
- A Formula field for margin amount (for example, `Example_Margin_Amount__c` = `NetTotalPrice - (Example_Unit_Cost__c * Quantity)`).
Extend the Context Definition
-
In the extended sales transaction context definition, add two attributes:
- An Input/Output String attribute at the transaction level to carry the cost book ID (for example, `ExampleCostBookId`).
- An Input/Output Currency attribute at the line-item level to carry the unit cost (for example, `ExampleUnitCost`).
- Add each attribute as a tag.
-
In the context mapping:
- Map the cost book ID tag to the custom Quote field (`Example_Cost_Book_Id__c`).
- Map the unit cost tag to the custom Quote Line Item field (`Example_Unit_Cost__c`).
Build a Decision Table to Look Up Cost
- Add the decision table to the pricing recipe, mapping its output to a unit price or cost field.
-
In the pricing procedure, add a lookup element that uses this decision table:
- Map the cost book ID and product for the current line as inputs.
- Map the output result to the `ExampleUnitCost` tag created in the context definition.
- Save and activate the pricing procedure.
Display Cost and Margin on the Quote
Add the custom Quote Line Item fields (`Example_Unit_Cost__c`, `Example_Total_Cost__c`, `Example_Margin_Percent__c`, and `Example_Margin_Amount__c`) to the line item layout or the Transaction Line Editor so sales representatives can view them.
Test the Configuration
Create a quote, add a product that has a cost book entry, and confirm that the cost, margin percentage, and margin amount gets calculated as expected. If a value doesn't populate, verify that the context mapping matches the target object type and that the decision table is active.
