unicodeCpp / Salesforce Platform APIs / Calculate Price (New Sale with Discounts)
Back to Collection Items
#include <CkHttpW.h>
#include <CkJsonObjectW.h>
#include <CkHttpResponseW.h>
void ChilkatSample(void)
{
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkHttpW http;
bool success;
// 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"
// }
// }
// ]
// }
// }
CkJsonObjectW json;
json.UpdateString(L"listPricebookId",L"{{standardPricebook}}");
json.UpdateString(L"pricingFlow",L"GET_PRICE_USING_CONTEXT");
json.UpdateString(L"graph.graphId",L"1");
json.UpdateString(L"graph.records[0].referenceId",L"ref_sales_txn");
json.UpdateString(L"graph.records[0].record.attributes.type",L"SalesTransactionShape");
json.UpdateString(L"graph.records[0].record.CurrencyIsoCode",L"USD");
json.UpdateString(L"graph.records[1].referenceId",L"Item_1_Evergreen");
json.UpdateString(L"graph.records[1].record.attributes.type",L"SalesTransactionItemShape");
json.UpdateString(L"graph.records[1].record.SalesTransactionShapeId",L"@{ref_sales_txn.Id}");
json.UpdateString(L"graph.records[1].record.StartDate",L"2023-01-01");
json.UpdateString(L"graph.records[1].record.ProductId",L"{{SLA_BronzeProduct}}");
json.UpdateString(L"graph.records[1].record.ProductSellingModelId",L"{{EvergreenMonthlyPSM}}");
json.UpdateNumber(L"graph.records[1].record.Quantity",L"3.0");
json.UpdateString(L"graph.records[1].record.PricingTransactionType",L"NewSale");
json.UpdateString(L"graph.records[1].record.CurrencyIsoCode",L"USD");
json.UpdateString(L"graph.records[2].referenceId",L"Item_2_Termed");
json.UpdateString(L"graph.records[2].record.attributes.type",L"SalesTransactionItemShape");
json.UpdateString(L"graph.records[2].record.SalesTransactionShapeId",L"@{ref_sales_txn.Id}");
json.UpdateString(L"graph.records[2].record.StartDate",L"2023-01-01");
json.UpdateString(L"graph.records[2].record.EndDate",L"2024-01-01");
json.UpdateString(L"graph.records[2].record.ProductId",L"{{VirtualRouterProduct}}");
json.UpdateString(L"graph.records[2].record.ProductSellingModelId",L"{{TermMonthlyPSM}}");
json.UpdateNumber(L"graph.records[2].record.Quantity",L"100.0");
json.UpdateString(L"graph.records[2].record.PricingTransactionType",L"NewSale");
json.UpdateString(L"graph.records[2].record.CurrencyIsoCode",L"USD");
json.UpdateString(L"graph.records[3].referenceId",L"Order_Level_Discount");
json.UpdateString(L"graph.records[3].record.attributes.type",L"PriceAdjustmentGroupShape");
json.UpdateString(L"graph.records[3].record.SalesTransactionShapeId",L"@{ref_sales_txn.Id}");
json.UpdateString(L"graph.records[3].record.AdjustmentType",L"AdjustmentPercentage");
json.UpdateInt(L"graph.records[3].record.AdjustmentValue",-20);
json.UpdateString(L"graph.records[3].record.AdjustmentSource",L"Discretionary");
json.UpdateString(L"graph.records[4].referenceId",L"Line_Level_Discount");
json.UpdateString(L"graph.records[4].record.attributes.type",L"PriceAdjustmentItemShape");
json.UpdateString(L"graph.records[4].record.SalesTransactionItemShapeId",L"@{Item_1_Evergreen.Id}");
json.UpdateString(L"graph.records[4].record.AdjustmentType",L"AdjustmentAmount");
json.UpdateInt(L"graph.records[4].record.AdjustmentValue",-10);
json.UpdateString(L"graph.records[4].record.AdjustmentSource",L"Discretionary");
json.UpdateString(L"graph.records[4].record.AdjustmentAmountScope",L"Unit");
// Adds the "Authorization: Bearer <access_token>" header.
http.put_AuthToken(L"<access_token>");
http.SetRequestHeader(L"Content-Type",L"application/json");
CkHttpResponseW *resp = http.PostJson3(L"https://domain.com/services/data/v{{version}}/commerce/pricing/salestransaction/actions/calculate-price",L"application/json",json);
if (http.get_LastMethodSuccess() == false) {
wprintf(L"%s\n",http.lastErrorText());
return;
}
wprintf(L"%d\n",resp->get_StatusCode());
wprintf(L"%s\n",resp->bodyStr());
delete resp;
}
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": [
]
}