Chilkat Online Tools

PureBasic / Zoho CRM REST APIs / Updating an inventory module's record with external fields in the line items

Back to Collection Items

IncludeFile "CkJsonObject.pb"
IncludeFile "CkStringBuilder.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.

    ; {
    ;   "data": [
    ;     {
    ;       "Product_Details": [
    ;         {
    ;           "id": "111111000000133071"
    ;         },
    ;         {
    ;           "product": {
    ;             "External_Product_ID": "12345678900"
    ;           },
    ;           "book": {
    ;             "External_Pricebook_ID": "pricebook2"
    ;           },
    ;           "quantity": 20
    ;         }
    ;       ]
    ;     }
    ;   ]
    ; }

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

    CkJsonObject::ckUpdateString(json,"data[0].Product_Details[0].id","111111000000133071")
    CkJsonObject::ckUpdateString(json,"data[0].Product_Details[1].product.External_Product_ID","12345678900")
    CkJsonObject::ckUpdateString(json,"data[0].Product_Details[1].book.External_Pricebook_ID","pricebook2")
    CkJsonObject::ckUpdateInt(json,"data[0].Product_Details[1].quantity",20)

    ; Adds the "Authorization: Bearer <access_token>" header.
    CkHttp::setCkAuthToken(http, "<access_token>")
    CkHttp::ckSetRequestHeader(http,"X-EXTERNAL","Quotes.Product_Details.product.External_Product_ID, Quotes.Product_Details.book.External_Price_book_ID")

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

    CkJsonObject::ckEmitSb(json,sbRequestBody)

    resp.i = CkHttp::ckPTextSb(http,"PUT","https://domain.com/crm/v2/Quotes/quote1",sbRequestBody,"utf-8","application/json",0,0)
    If CkHttp::ckLastMethodSuccess(http) = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbRequestBody)
        ProcedureReturn
    EndIf

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



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


    ProcedureReturn
EndProcedure

Curl Command

curl -X PUT
	-H "Authorization: Bearer <access_token>"
	-H "X-EXTERNAL: Quotes.Product_Details.product.External_Product_ID, Quotes.Product_Details.book.External_Price_book_ID"
	-d '{
    "data": [
        {
            "Product_Details": [
                {
                    "id": "111111000000133071"
                },
                {
                    "product": {
                        "External_Product_ID": "12345678900"
                    },
                    "book": {
                        "External_Pricebook_ID": "pricebook2"
                    },
                    "quantity": 20
                }
            ]
        }
    ]
}'
https://domain.com/crm/v2/Quotes/quote1

Postman Collection Item JSON

{
  "name": "Updating an inventory module's record with external fields in the line items",
  "request": {
    "auth": {
      "type": "bearer",
      "bearer": [
        {
          "key": "token",
          "value": "{{access-token}}",
          "type": "string"
        }
      ]
    },
    "method": "PUT",
    "header": [
      {
        "key": "X-EXTERNAL",
        "value": "Quotes.Product_Details.product.External_Product_ID, Quotes.Product_Details.book.External_Price_book_ID",
        "type": "text"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"data\": [\n        {\n            \"Product_Details\": [\n                {\n                    \"id\": \"111111000000133071\"\n                },\n                {\n                    \"product\": {\n                        \"External_Product_ID\": \"12345678900\"\n                    },\n                    \"book\": {\n                        \"External_Pricebook_ID\": \"pricebook2\"\n                    },\n                    \"quantity\": 20\n                }\n            ]\n        }\n    ]\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{api-domain}}/crm/v2/Quotes/quote1",
      "host": [
        "{{api-domain}}"
      ],
      "path": [
        "crm",
        "v2",
        "Quotes",
        "quote1"
      ]
    },
    "description": "In this example, Quotes is the base module. To update the details in the Product_Details section, specify the external field's API name and the value in the product and book JSON objects."
  },
  "response": [
  ]
}