SQL Server / Salesforce Platform APIs / Calculate Price (New Sale with Discounts)
Back to Collection Items
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
DECLARE @http int
-- Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
DECLARE @success int
-- Use this online tool to generate code from sample JSON: Generate Code to Create JSON
-- The following JSON is sent in the request body.
-- {
-- "listPricebookId": "{{standardPricebook}}",
-- "pricingFlow": "GET_PRICE_USING_CONTEXT",
-- "graph": {
-- "graphId": "1",
-- "records": [
-- {
-- "referenceId": "ref_sales_txn",
-- "record": {
-- "attributes": {
-- "type": "SalesTransactionShape"
-- },
-- "CurrencyIsoCode": "USD"
-- }
-- },
-- {
-- "referenceId": "Item_1_Evergreen",
-- "record": {
-- "attributes": {
-- "type": "SalesTransactionItemShape"
-- },
-- "SalesTransactionShapeId": "@{ref_sales_txn.Id}",
-- "StartDate": "2023-01-01",
-- "ProductId": "{{SLA_BronzeProduct}}",
-- "ProductSellingModelId": "{{EvergreenMonthlyPSM}}",
-- "Quantity": 3.0,
-- "PricingTransactionType": "NewSale",
-- "CurrencyIsoCode": "USD"
-- }
-- },
-- {
-- "referenceId": "Item_2_Termed",
-- "record": {
-- "attributes": {
-- "type": "SalesTransactionItemShape"
-- },
-- "SalesTransactionShapeId": "@{ref_sales_txn.Id}",
-- "StartDate": "2023-01-01",
-- "EndDate": "2024-01-01",
-- "ProductId": "{{VirtualRouterProduct}}",
-- "ProductSellingModelId": "{{TermMonthlyPSM}}",
-- "Quantity": 100.0,
-- "PricingTransactionType": "NewSale",
-- "CurrencyIsoCode": "USD"
-- }
-- },
-- {
-- "referenceId": "Order_Level_Discount",
-- "record": {
-- "attributes": {
-- "type": "PriceAdjustmentGroupShape"
-- },
-- "SalesTransactionShapeId": "@{ref_sales_txn.Id}",
-- "AdjustmentType": "AdjustmentPercentage",
-- "AdjustmentValue": -20,
-- "AdjustmentSource": "Discretionary"
-- }
-- },
-- {
-- "referenceId": "Line_Level_Discount",
-- "record": {
-- "attributes": {
-- "type": "PriceAdjustmentItemShape"
-- },
-- "SalesTransactionItemShapeId": "@{Item_1_Evergreen.Id}",
-- "AdjustmentType": "AdjustmentAmount",
-- "AdjustmentValue": -10,
-- "AdjustmentSource": "Discretionary",
-- "AdjustmentAmountScope": "Unit"
-- }
-- }
-- ]
-- }
-- }
DECLARE @json int
-- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'listPricebookId', '{{standardPricebook}}'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'pricingFlow', 'GET_PRICE_USING_CONTEXT'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graph.graphId', '1'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graph.records[0].referenceId', 'ref_sales_txn'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graph.records[0].record.attributes.type', 'SalesTransactionShape'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graph.records[0].record.CurrencyIsoCode', 'USD'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graph.records[1].referenceId', 'Item_1_Evergreen'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graph.records[1].record.attributes.type', 'SalesTransactionItemShape'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graph.records[1].record.SalesTransactionShapeId', '@{ref_sales_txn.Id}'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graph.records[1].record.StartDate', '2023-01-01'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graph.records[1].record.ProductId', '{{SLA_BronzeProduct}}'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graph.records[1].record.ProductSellingModelId', '{{EvergreenMonthlyPSM}}'
EXEC sp_OAMethod @json, 'UpdateNumber', @success OUT, 'graph.records[1].record.Quantity', '3.0'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graph.records[1].record.PricingTransactionType', 'NewSale'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graph.records[1].record.CurrencyIsoCode', 'USD'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graph.records[2].referenceId', 'Item_2_Termed'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graph.records[2].record.attributes.type', 'SalesTransactionItemShape'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graph.records[2].record.SalesTransactionShapeId', '@{ref_sales_txn.Id}'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graph.records[2].record.StartDate', '2023-01-01'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graph.records[2].record.EndDate', '2024-01-01'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graph.records[2].record.ProductId', '{{VirtualRouterProduct}}'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graph.records[2].record.ProductSellingModelId', '{{TermMonthlyPSM}}'
EXEC sp_OAMethod @json, 'UpdateNumber', @success OUT, 'graph.records[2].record.Quantity', '100.0'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graph.records[2].record.PricingTransactionType', 'NewSale'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graph.records[2].record.CurrencyIsoCode', 'USD'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graph.records[3].referenceId', 'Order_Level_Discount'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graph.records[3].record.attributes.type', 'PriceAdjustmentGroupShape'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graph.records[3].record.SalesTransactionShapeId', '@{ref_sales_txn.Id}'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graph.records[3].record.AdjustmentType', 'AdjustmentPercentage'
EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'graph.records[3].record.AdjustmentValue', -20
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graph.records[3].record.AdjustmentSource', 'Discretionary'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graph.records[4].referenceId', 'Line_Level_Discount'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graph.records[4].record.attributes.type', 'PriceAdjustmentItemShape'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graph.records[4].record.SalesTransactionItemShapeId', '@{Item_1_Evergreen.Id}'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graph.records[4].record.AdjustmentType', 'AdjustmentAmount'
EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'graph.records[4].record.AdjustmentValue', -10
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graph.records[4].record.AdjustmentSource', 'Discretionary'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graph.records[4].record.AdjustmentAmountScope', 'Unit'
-- Adds the "Authorization: Bearer <access_token>" header.
EXEC sp_OASetProperty @http, 'AuthToken', '<access_token>'
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'application/json'
DECLARE @resp int
EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://domain.com/services/data/v{{version}}/commerce/pricing/salestransaction/actions/calculate-price', 'application/json', @json
EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @json
RETURN
END
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
PRINT @iTmp0
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @json
END
GO
Curl Command
curl -X POST
-H "Authorization: Bearer <access_token>"
-H "Content-Type: application/json"
-d '{
"listPricebookId": "{{standardPricebook}}",
"pricingFlow": "GET_PRICE_USING_CONTEXT",
"graph": {
"graphId": "1",
"records": [
{
"referenceId": "ref_sales_txn",
"record": {
"attributes": {
"type": "SalesTransactionShape"
},
"CurrencyIsoCode": "USD"
}
},
{
"referenceId": "Item_1_Evergreen",
"record": {
"attributes": {
"type": "SalesTransactionItemShape"
},
"SalesTransactionShapeId": "@{ref_sales_txn.Id}",
"StartDate": "2023-01-01",
"ProductId": "{{SLA_BronzeProduct}}",
"ProductSellingModelId": "{{EvergreenMonthlyPSM}}",
"Quantity": 3.0,
"PricingTransactionType": "NewSale",
"CurrencyIsoCode": "USD"
}
},
{
"referenceId": "Item_2_Termed",
"record": {
"attributes": {
"type": "SalesTransactionItemShape"
},
"SalesTransactionShapeId": "@{ref_sales_txn.Id}",
"StartDate": "2023-01-01",
"EndDate": "2024-01-01",
"ProductId": "{{VirtualRouterProduct}}",
"ProductSellingModelId": "{{TermMonthlyPSM}}",
"Quantity": 100.0,
"PricingTransactionType": "NewSale",
"CurrencyIsoCode": "USD"
}
},
{
"referenceId": "Order_Level_Discount",
"record": {
"attributes": {
"type": "PriceAdjustmentGroupShape"
},
"SalesTransactionShapeId": "@{ref_sales_txn.Id}",
"AdjustmentType": "AdjustmentPercentage",
"AdjustmentValue": -20,
"AdjustmentSource": "Discretionary"
}
},
{
"referenceId": "Line_Level_Discount",
"record": {
"attributes": {
"type": "PriceAdjustmentItemShape"
},
"SalesTransactionItemShapeId": "@{Item_1_Evergreen.Id}",
"AdjustmentType": "AdjustmentAmount",
"AdjustmentValue": -10,
"AdjustmentSource": "Discretionary",
"AdjustmentAmountScope": "Unit"
}
}
]
}
}'
https://domain.com/services/data/v{{version}}/commerce/pricing/salestransaction/actions/calculate-price
Postman Collection Item JSON
{
"name": "Calculate Price (New Sale with Discounts)",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"listPricebookId\": \"{{standardPricebook}}\",\n \"pricingFlow\": \"GET_PRICE_USING_CONTEXT\",\n \"graph\": {\n \"graphId\": \"1\",\n \"records\": [\n {\n \"referenceId\": \"ref_sales_txn\",\n \"record\": {\n \"attributes\": {\n \"type\": \"SalesTransactionShape\"\n },\n \"CurrencyIsoCode\": \"USD\"\n }\n },\n {\n \"referenceId\": \"Item_1_Evergreen\",\n \"record\": {\n \"attributes\": {\n \"type\": \"SalesTransactionItemShape\"\n },\n \"SalesTransactionShapeId\": \"@{ref_sales_txn.Id}\",\n \"StartDate\": \"2023-01-01\",\n \"ProductId\": \"{{SLA_BronzeProduct}}\",\n \"ProductSellingModelId\": \"{{EvergreenMonthlyPSM}}\",\n \"Quantity\": 3.0,\n \"PricingTransactionType\": \"NewSale\",\n \"CurrencyIsoCode\": \"USD\"\n }\n },\n {\n \"referenceId\": \"Item_2_Termed\",\n \"record\": {\n \"attributes\": {\n \"type\": \"SalesTransactionItemShape\"\n },\n \"SalesTransactionShapeId\": \"@{ref_sales_txn.Id}\",\n \"StartDate\": \"2023-01-01\",\n \"EndDate\": \"2024-01-01\",\n \"ProductId\": \"{{VirtualRouterProduct}}\",\n \"ProductSellingModelId\": \"{{TermMonthlyPSM}}\",\n \"Quantity\": 100.0,\n \"PricingTransactionType\": \"NewSale\",\n \"CurrencyIsoCode\": \"USD\"\n }\n },\n {\n \"referenceId\": \"Order_Level_Discount\",\n \"record\": {\n \"attributes\": {\n \"type\": \"PriceAdjustmentGroupShape\"\n },\n \"SalesTransactionShapeId\": \"@{ref_sales_txn.Id}\",\n \"AdjustmentType\": \"AdjustmentPercentage\",\n \"AdjustmentValue\": -20,\n \"AdjustmentSource\": \"Discretionary\"\n }\n },\n {\n \"referenceId\": \"Line_Level_Discount\",\n \"record\": {\n \"attributes\": {\n \"type\": \"PriceAdjustmentItemShape\"\n },\n \"SalesTransactionItemShapeId\": \"@{Item_1_Evergreen.Id}\",\n \"AdjustmentType\": \"AdjustmentAmount\",\n \"AdjustmentValue\": -10,\n \"AdjustmentSource\": \"Discretionary\",\n \"AdjustmentAmountScope\": \"Unit\"\n }\n } \n ]\n }\n}"
},
"url": {
"raw": "{{_endpoint}}/services/data/v{{version}}/commerce/pricing/salestransaction/actions/calculate-price",
"host": [
"{{_endpoint}}"
],
"path": [
"services",
"data",
"v{{version}}",
"commerce",
"pricing",
"salestransaction",
"actions",
"calculate-price"
]
},
"description": "Calculate the price of a sales transaction, including a top-level and a line-level discount. The top-level discount specifies 20% off the entire sales transaction, while the line-level discount specifies $10 off the sales transaction item.\n\nIn this example, the sales transaction is an order and the sales transaction item is an order item."
},
"response": [
]
}