Chilkat Online Tools

SQL Server / Zoho CRM REST APIs / Territories

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
    -- 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

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Authorization', '{{authorization-token}}'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'application/json'

    DECLARE @sbResponseBody int
    -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @http, 'QuickGetSb', @success OUT, 'https://domain.com/crm/v2/settings/territories', @sbResponseBody
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @sbResponseBody
        RETURN
      END

    DECLARE @jResp int
    -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jResp OUT

    EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
    EXEC sp_OASetProperty @jResp, 'EmitCompact', 0


    PRINT 'Response Body:'
    EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @http, 'LastStatus', @respStatusCode OUT

    PRINT 'Response Status Code = ' + @respStatusCode
    IF @respStatusCode >= 400
      BEGIN

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @http, 'LastHeader', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @sbResponseBody
        EXEC @hr = sp_OADestroy @jResp
        RETURN
      END

    -- Sample JSON response:
    -- (Sample code for parsing the JSON response is shown below)

    -- {
    --   "territories": [
    --     {
    --       "created_time": "2021-03-18T09:21:33+00:00",
    --       "modified_time": "2021-03-18T09:21:33+00:00",
    --       "manager": {
    --         "name": "Patricia Boyle",
    --         "id": "738964000000291009"
    --       },
    --       "parent_id": null,
    --       "criteria": null,
    --       "name": "Zylker Inc.",
    --       "modified_by": {
    --         "name": "Patricia Boyle",
    --         "id": "738964000000291009"
    --       },
    --       "description": "Organization Parent Territory",
    --       "id": "738964000001977341",
    --       "created_by": {
    --         "name": "Patricia Boyle",
    --         "id": "738964000000291009"
    --       }
    --     },
    --     {
    --       "created_time": "2021-03-18T09:21:55+00:00",
    --       "modified_time": "2021-03-18T09:21:55+00:00",
    --       "manager": null,
    --       "parent_id": "738964000001977341",
    --       "criteria": {
    --         "comparator": "contains",
    --         "field": "Account_Name",
    --         "value": "Zylker"
    --       },
    --       "name": "South Zone",
    --       "modified_by": {
    --         "name": "Patricia Boyle",
    --         "id": "738964000000291009"
    --       },
    --       "description": null,
    --       "id": "738964000001977355",
    --       "created_by": {
    --         "name": "Patricia Boyle",
    --         "id": "738964000000291009"
    --       }
    --     }
    --   ],
    --   "info": {
    --     "per_page": 200,
    --     "count": 2,
    --     "page": 1,
    --     "more_records": false
    --   }
    -- }

    -- Sample code for parsing the JSON response...
    -- Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

    DECLARE @created_time nvarchar(4000)

    DECLARE @modified_time nvarchar(4000)

    DECLARE @Name nvarchar(4000)

    DECLARE @Id nvarchar(4000)

    DECLARE @parent_id nvarchar(4000)

    DECLARE @criteria nvarchar(4000)

    DECLARE @name nvarchar(4000)

    DECLARE @modified_byName nvarchar(4000)

    DECLARE @modified_byId nvarchar(4000)

    DECLARE @description nvarchar(4000)

    DECLARE @id nvarchar(4000)

    DECLARE @created_byName nvarchar(4000)

    DECLARE @created_byId nvarchar(4000)

    DECLARE @manager nvarchar(4000)

    DECLARE @Comparator nvarchar(4000)

    DECLARE @Field nvarchar(4000)

    DECLARE @Value nvarchar(4000)

    DECLARE @Per_page int
    EXEC sp_OAMethod @jResp, 'IntOf', @Per_page OUT, 'info.per_page'
    DECLARE @Count int
    EXEC sp_OAMethod @jResp, 'IntOf', @Count OUT, 'info.count'
    DECLARE @Page int
    EXEC sp_OAMethod @jResp, 'IntOf', @Page OUT, 'info.page'
    DECLARE @More_records int
    EXEC sp_OAMethod @jResp, 'BoolOf', @More_records OUT, 'info.more_records'
    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'territories'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @created_time OUT, 'territories[i].created_time'
        EXEC sp_OAMethod @jResp, 'StringOf', @modified_time OUT, 'territories[i].modified_time'
        EXEC sp_OAMethod @jResp, 'StringOf', @Name OUT, 'territories[i].manager.name'
        EXEC sp_OAMethod @jResp, 'StringOf', @Id OUT, 'territories[i].manager.id'
        EXEC sp_OAMethod @jResp, 'StringOf', @parent_id OUT, 'territories[i].parent_id'
        EXEC sp_OAMethod @jResp, 'StringOf', @criteria OUT, 'territories[i].criteria'
        EXEC sp_OAMethod @jResp, 'StringOf', @name OUT, 'territories[i].name'
        EXEC sp_OAMethod @jResp, 'StringOf', @modified_byName OUT, 'territories[i].modified_by.name'
        EXEC sp_OAMethod @jResp, 'StringOf', @modified_byId OUT, 'territories[i].modified_by.id'
        EXEC sp_OAMethod @jResp, 'StringOf', @description OUT, 'territories[i].description'
        EXEC sp_OAMethod @jResp, 'StringOf', @id OUT, 'territories[i].id'
        EXEC sp_OAMethod @jResp, 'StringOf', @created_byName OUT, 'territories[i].created_by.name'
        EXEC sp_OAMethod @jResp, 'StringOf', @created_byId OUT, 'territories[i].created_by.id'
        EXEC sp_OAMethod @jResp, 'StringOf', @manager OUT, 'territories[i].manager'
        EXEC sp_OAMethod @jResp, 'StringOf', @Comparator OUT, 'territories[i].criteria.comparator'
        EXEC sp_OAMethod @jResp, 'StringOf', @Field OUT, 'territories[i].criteria.field'
        EXEC sp_OAMethod @jResp, 'StringOf', @Value OUT, 'territories[i].criteria.value'
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jResp


END
GO

Curl Command

curl -X GET
	-H "Authorization: Bearer <access_token>"
	-H "Authorization: {{authorization-token}}"
	-H "Content-Type: application/json"
https://domain.com/crm/v2/settings/territories

Postman Collection Item JSON

{
  "name": "Territories",
  "request": {
    "auth": {
      "type": "bearer",
      "bearer": [
        {
          "key": "token",
          "value": "{{access-token}}",
          "type": "string"
        }
      ]
    },
    "method": "GET",
    "header": [
      {
        "key": "Authorization",
        "value": "{{authorization-token}}"
      },
      {
        "key": "Content-Type",
        "name": "Content-Type",
        "value": "application/json",
        "type": "text"
      }
    ],
    "url": {
      "raw": "{{api-domain}}/crm/v2/settings/territories",
      "host": [
        "{{api-domain}}"
      ],
      "path": [
        "crm",
        "v2",
        "settings",
        "territories"
      ]
    },
    "description": "To get the list of territories enabled for your organization."
  },
  "response": [
    {
      "name": "SUCCESS RESPONSE",
      "originalRequest": {
        "method": "GET",
        "header": [
          {
            "key": "Authorization",
            "value": "{{authorization-token}}"
          },
          {
            "key": "Content-Type",
            "name": "Content-Type",
            "value": "application/json",
            "type": "text"
          }
        ],
        "url": {
          "raw": "{{api-domain}}/crm/v2/settings/territories",
          "host": [
            "{{api-domain}}"
          ],
          "path": [
            "crm",
            "v2",
            "settings",
            "territories"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "ZGS"
        },
        {
          "key": "Date",
          "value": "Tue, 11 May 2021 12:20:18 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json;charset=utf-8"
        },
        {
          "key": "Content-Length",
          "value": "908"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "Referrer-Policy",
          "value": "strict-origin"
        },
        {
          "key": "X-Content-Type-Options",
          "value": "nosniff"
        },
        {
          "key": "X-XSS-Protection",
          "value": "1; mode=block"
        },
        {
          "key": "Pragma",
          "value": "no-cache"
        },
        {
          "key": "Cache-Control",
          "value": "no-store, no-cache, must-revalidate, private"
        },
        {
          "key": "Expires",
          "value": "Thu, 01 Jan 1970 00:00:00 GMT"
        },
        {
          "key": "X-Frame-Options",
          "value": "SAMEORIGIN"
        },
        {
          "key": "X-ACCESSTOKEN-RESET",
          "value": "2021-05-11T12:57:36+00:00"
        },
        {
          "key": "clientVersion",
          "value": "4023320"
        },
        {
          "key": "clientsubVersion",
          "value": "4d8b6ee4f7d1d284c930a4e807480c5c"
        },
        {
          "key": "Content-Disposition",
          "value": "attachment; filename=response.json"
        },
        {
          "key": "Content-Language",
          "value": "en-US"
        },
        {
          "key": "Strict-Transport-Security",
          "value": "max-age=15768000"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"territories\": [\n        {\n            \"created_time\": \"2021-03-18T09:21:33+00:00\",\n            \"modified_time\": \"2021-03-18T09:21:33+00:00\",\n            \"manager\": {\n                \"name\": \"Patricia Boyle\",\n                \"id\": \"738964000000291009\"\n            },\n            \"parent_id\": null,\n            \"criteria\": null,\n            \"name\": \"Zylker Inc.\",\n            \"modified_by\": {\n                \"name\": \"Patricia Boyle\",\n                \"id\": \"738964000000291009\"\n            },\n            \"description\": \"Organization Parent Territory\",\n            \"id\": \"738964000001977341\",\n            \"created_by\": {\n                \"name\": \"Patricia Boyle\",\n                \"id\": \"738964000000291009\"\n            }\n        },\n        {\n            \"created_time\": \"2021-03-18T09:21:55+00:00\",\n            \"modified_time\": \"2021-03-18T09:21:55+00:00\",\n            \"manager\": null,\n            \"parent_id\": \"738964000001977341\",\n            \"criteria\": {\n                \"comparator\": \"contains\",\n                \"field\": \"Account_Name\",\n                \"value\": \"Zylker\"\n            },\n            \"name\": \"South Zone\",\n            \"modified_by\": {\n                \"name\": \"Patricia Boyle\",\n                \"id\": \"738964000000291009\"\n            },\n            \"description\": null,\n            \"id\": \"738964000001977355\",\n            \"created_by\": {\n                \"name\": \"Patricia Boyle\",\n                \"id\": \"738964000000291009\"\n            }\n        }\n    ],\n    \"info\": {\n        \"per_page\": 200,\n        \"count\": 2,\n        \"page\": 1,\n        \"more_records\": false\n    }\n}"
    },
    {
      "name": "INVALID_URL_PATTERN",
      "originalRequest": {
        "method": "GET",
        "header": [
          {
            "key": "Authorization",
            "value": "{{authorization-token}}"
          },
          {
            "key": "Content-Type",
            "name": "Content-Type",
            "value": "application/json",
            "type": "text"
          }
        ],
        "url": {
          "raw": "{{api-domain}}/crm/v2/settings/territorie",
          "host": [
            "{{api-domain}}"
          ],
          "path": [
            "crm",
            "v2",
            "settings",
            "territorie"
          ]
        }
      },
      "status": "Not Found",
      "code": 404,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "ZGS"
        },
        {
          "key": "Date",
          "value": "Tue, 11 May 2021 12:21:05 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json;charset=utf-8"
        },
        {
          "key": "Content-Length",
          "value": "131"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "Referrer-Policy",
          "value": "strict-origin"
        },
        {
          "key": "X-Content-Type-Options",
          "value": "nosniff"
        },
        {
          "key": "X-XSS-Protection",
          "value": "1; mode=block"
        },
        {
          "key": "Pragma",
          "value": "no-cache"
        },
        {
          "key": "Cache-Control",
          "value": "no-store, no-cache, must-revalidate, private"
        },
        {
          "key": "Expires",
          "value": "Thu, 01 Jan 1970 00:00:00 GMT"
        },
        {
          "key": "X-Frame-Options",
          "value": "SAMEORIGIN"
        },
        {
          "key": "X-ACCESSTOKEN-RESET",
          "value": "2021-05-11T12:57:36+00:00"
        },
        {
          "key": "Content-Disposition",
          "value": "attachment; filename=response.json"
        },
        {
          "key": "X-Download-Options",
          "value": "noopen"
        },
        {
          "key": "Content-Language",
          "value": "en-US"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"code\": \"INVALID_URL_PATTERN\",\n    \"details\": {},\n    \"message\": \"Please check if the URL trying to access is a correct one\",\n    \"status\": \"error\"\n}"
    },
    {
      "name": "INVALID_REQUEST_METHOD",
      "originalRequest": {
        "method": "COPY",
        "header": [
          {
            "key": "Authorization",
            "value": "{{authorization-token}}"
          },
          {
            "key": "Content-Type",
            "name": "Content-Type",
            "value": "application/json",
            "type": "text"
          }
        ],
        "url": {
          "raw": "{{api-domain}}/crm/v2/settings/territorie",
          "host": [
            "{{api-domain}}"
          ],
          "path": [
            "crm",
            "v2",
            "settings",
            "territorie"
          ]
        }
      },
      "status": "Bad Request",
      "code": 400,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "ZGS"
        },
        {
          "key": "Date",
          "value": "Tue, 11 May 2021 12:21:20 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json;charset=utf-8"
        },
        {
          "key": "Content-Length",
          "value": "124"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "X-Frame-Options",
          "value": "deny"
        },
        {
          "key": "X-Download-Options",
          "value": "noopen"
        },
        {
          "key": "X-Content-Type-Options",
          "value": "nosniff"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"code\": \"INVALID_REQUEST_METHOD\",\n    \"details\": {},\n    \"message\": \"The http request method type is not a valid one\",\n    \"status\": \"error\"\n}"
    },
    {
      "name": "AUTHENTICATION_FAILURE",
      "originalRequest": {
        "method": "GET",
        "header": [
          {
            "key": "Authorization",
            "value": "{{authorization-token}}"
          },
          {
            "key": "Content-Type",
            "name": "Content-Type",
            "value": "application/json",
            "type": "text"
          }
        ],
        "url": {
          "raw": "{{api-domain}}/crm/v2/settings/territories",
          "host": [
            "{{api-domain}}"
          ],
          "path": [
            "crm",
            "v2",
            "settings",
            "territories"
          ]
        }
      },
      "status": "Unauthorized",
      "code": 401,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "ZGS"
        },
        {
          "key": "Date",
          "value": "Tue, 11 May 2021 12:21:44 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json;charset=utf-8"
        },
        {
          "key": "Content-Length",
          "value": "98"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "Referrer-Policy",
          "value": "strict-origin"
        },
        {
          "key": "X-Content-Type-Options",
          "value": "nosniff"
        },
        {
          "key": "X-XSS-Protection",
          "value": "1; mode=block"
        },
        {
          "key": "Pragma",
          "value": "no-cache"
        },
        {
          "key": "Cache-Control",
          "value": "no-cache"
        },
        {
          "key": "Expires",
          "value": "Thu, 01 Jan 1970 00:00:00 GMT"
        },
        {
          "key": "X-Frame-Options",
          "value": "SAMEORIGIN"
        },
        {
          "key": "X-Download-Options",
          "value": "noopen"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"code\": \"AUTHENTICATION_FAILURE\",\n    \"details\": {},\n    \"message\": \"Authentication failed\",\n    \"status\": \"error\"\n}"
    },
    {
      "name": "OAUTH_SCOPE_MISMATCH",
      "originalRequest": {
        "method": "GET",
        "header": [
          {
            "key": "Authorization",
            "value": "{{authorization-token}}"
          },
          {
            "key": "Content-Type",
            "name": "Content-Type",
            "value": "application/json",
            "type": "text"
          }
        ],
        "url": {
          "raw": "{{api-domain}}/crm/v2/settings/territories",
          "host": [
            "{{api-domain}}"
          ],
          "path": [
            "crm",
            "v2",
            "settings",
            "territories"
          ]
        }
      },
      "status": "Unauthorized",
      "code": 401,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "ZGS"
        },
        {
          "key": "Date",
          "value": "Thu, 13 May 2021 08:28:41 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json;charset=utf-8"
        },
        {
          "key": "Content-Length",
          "value": "113"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "Referrer-Policy",
          "value": "strict-origin"
        },
        {
          "key": "X-Content-Type-Options",
          "value": "nosniff"
        },
        {
          "key": "X-XSS-Protection",
          "value": "1; mode=block"
        },
        {
          "key": "Pragma",
          "value": "no-cache"
        },
        {
          "key": "Cache-Control",
          "value": "no-cache"
        },
        {
          "key": "Expires",
          "value": "Thu, 01 Jan 1970 00:00:00 GMT"
        },
        {
          "key": "X-Frame-Options",
          "value": "SAMEORIGIN"
        },
        {
          "key": "X-Download-Options",
          "value": "noopen"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"code\": \"OAUTH_SCOPE_MISMATCH\",\n    \"details\": {},\n    \"message\": \"invalid oauth scope to access this URL\",\n    \"status\": \"error\"\n}"
    }
  ]
}