Chilkat Online Tools

PureBasic / Salesforce Platform APIs / Calculate Price (New Sale)

Back to Collection Items

IncludeFile "CkJsonObject.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkHttpResponse.pb"

Procedure ChilkatExample()

    ; This example assumes the Chilkat API to have been previously unlocked.
    ; See Global Unlock Sample for sample code.

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success.i

    ; 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": "2023-12-31",
    ;           "ProductId": "{{VirtualRouterProduct}}",
    ;           "ProductSellingModelId": "{{TermMonthlyPSM}}",
    ;           "Quantity": 100.0,
    ;           "PricingTransactionType": "NewSale",
    ;           "CurrencyIsoCode": "USD"
    ;         }
    ;       }
    ;     ]
    ;   }
    ; }

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(json,"listPricebookId","{{standardPricebook}}")
    CkJsonObject::ckUpdateString(json,"pricingFlow","GET_PRICE_USING_CONTEXT")
    CkJsonObject::ckUpdateString(json,"graph.graphId","1")
    CkJsonObject::ckUpdateString(json,"graph.records[0].referenceId","ref_sales_txn")
    CkJsonObject::ckUpdateString(json,"graph.records[0].record.attributes.type","SalesTransactionShape")
    CkJsonObject::ckUpdateString(json,"graph.records[0].record.CurrencyIsoCode","USD")
    CkJsonObject::ckUpdateString(json,"graph.records[1].referenceId","Item_1_Evergreen")
    CkJsonObject::ckUpdateString(json,"graph.records[1].record.attributes.type","SalesTransactionItemShape")
    CkJsonObject::ckUpdateString(json,"graph.records[1].record.SalesTransactionShapeId","@{ref_sales_txn.Id}")
    CkJsonObject::ckUpdateString(json,"graph.records[1].record.StartDate","2023-01-01")
    CkJsonObject::ckUpdateString(json,"graph.records[1].record.ProductId","{{SLA_BronzeProduct}}")
    CkJsonObject::ckUpdateString(json,"graph.records[1].record.ProductSellingModelId","{{EvergreenMonthlyPSM}}")
    CkJsonObject::ckUpdateNumber(json,"graph.records[1].record.Quantity","3.0")
    CkJsonObject::ckUpdateString(json,"graph.records[1].record.PricingTransactionType","NewSale")
    CkJsonObject::ckUpdateString(json,"graph.records[1].record.CurrencyIsoCode","USD")
    CkJsonObject::ckUpdateString(json,"graph.records[2].referenceId","Item_2_Termed")
    CkJsonObject::ckUpdateString(json,"graph.records[2].record.attributes.type","SalesTransactionItemShape")
    CkJsonObject::ckUpdateString(json,"graph.records[2].record.SalesTransactionShapeId","@{ref_sales_txn.Id}")
    CkJsonObject::ckUpdateString(json,"graph.records[2].record.StartDate","2023-01-01")
    CkJsonObject::ckUpdateString(json,"graph.records[2].record.EndDate","2023-12-31")
    CkJsonObject::ckUpdateString(json,"graph.records[2].record.ProductId","{{VirtualRouterProduct}}")
    CkJsonObject::ckUpdateString(json,"graph.records[2].record.ProductSellingModelId","{{TermMonthlyPSM}}")
    CkJsonObject::ckUpdateNumber(json,"graph.records[2].record.Quantity","100.0")
    CkJsonObject::ckUpdateString(json,"graph.records[2].record.PricingTransactionType","NewSale")
    CkJsonObject::ckUpdateString(json,"graph.records[2].record.CurrencyIsoCode","USD")

    ; Adds the "Authorization: Bearer <access_token>" header.
    CkHttp::setCkAuthToken(http, "<access_token>")
    CkHttp::ckSetRequestHeader(http,"Content-Type","application/json")

    resp.i = CkHttp::ckPostJson3(http,"https://domain.com/services/data/v{{version}}/commerce/pricing/salestransaction/actions/calculate-price","application/json",json)
    If CkHttp::ckLastMethodSuccess(http) = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    Debug Str(CkHttpResponse::ckStatusCode(resp))
    Debug CkHttpResponse::ckBodyStr(resp)
    CkHttpResponse::ckDispose(resp)



    CkHttp::ckDispose(http)
    CkJsonObject::ckDispose(json)


    ProcedureReturn
EndProcedure

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": "2023-12-31",
                    "ProductId": "{{VirtualRouterProduct}}",
                    "ProductSellingModelId": "{{TermMonthlyPSM}}",
                    "Quantity": 100.0,
                    "PricingTransactionType": "NewSale",
                    "CurrencyIsoCode": "USD"
                }
            }
        ]
    }
}'
https://domain.com/services/data/v{{version}}/commerce/pricing/salestransaction/actions/calculate-price

Postman Collection Item JSON

{
  "name": "Calculate Price (New Sale)",
  "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\": \"2023-12-31\",\n                    \"ProductId\": \"{{VirtualRouterProduct}}\",\n                    \"ProductSellingModelId\": \"{{TermMonthlyPSM}}\",\n                    \"Quantity\": 100.0,\n                    \"PricingTransactionType\": \"NewSale\",\n                    \"CurrencyIsoCode\": \"USD\"\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 new sale that contains two order items: a term-defined subscription order for a bronze-level SLA, and an evergreen subscription order for a virtual router. The payload for the term-defined order contains both a start date and an end date. Instead of an end date, you can also include a start date and a subscription term.\n\nSee [Calculate Price](https://developer.salesforce.com/docs/revenue/subscription-management/references/prices?meta=Summary)."
  },
  "response": [
  ]
}