Chilkat Online Tools

SQL Server / Egyptian eInvoicing SDK R1.5 / 6. Create EGS Code Usage

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.

    -- {
    --   "items": [
    --     {
    --       "codeType": "EGS",
    --       "parentCode": "10000051",
    --       "itemCode": "EG-113317713-5598542",
    --       "codeName": "Test Code",
    --       "codeNameAr": "كود تجريبي",
    --       "activeFrom": "2021-02-20T00:00:00.000",
    --       "activeTo": "2021-04-22T00:00:00.000",
    --       "description": "Description of code",
    --       "descriptionAr": "وصف الكود بالعربي",
    --       "requestReason": "Request reason text"
    --     },
    --     {
    --       "codeType": "EGS",
    --       "parentCode": "10000051",
    --       "itemCode": "EG-113317713-5598543",
    --       "codeName": "Test Code 1",
    --       "codeNameAr": "1 كود تجريبي",
    --       "activeFrom": "2021-02-20T00:00:00.000",
    --       "activeTo": "2021-04-22T00:00:00.000",
    --       "description": "Description of code 1",
    --       "descriptionAr": " 1 وصف الكود بالعربي",
    --       "requestReason": "Request reason text"
    --     }
    --   ]
    -- }

    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, 'items[0].codeType', 'EGS'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].parentCode', '10000051'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].itemCode', 'EG-113317713-5598542'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].codeName', 'Test Code'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].codeNameAr', 'كود تجريبي'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].activeFrom', '2021-02-20T00:00:00.000'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].activeTo', '2021-04-22T00:00:00.000'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].description', 'Description of code'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].descriptionAr', 'وصف الكود بالعربي'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].requestReason', 'Request reason text'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].codeType', 'EGS'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].parentCode', '10000051'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].itemCode', 'EG-113317713-5598543'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].codeName', 'Test Code 1'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].codeNameAr', '1 كود تجريبي'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].activeFrom', '2021-02-20T00:00:00.000'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].activeTo', '2021-04-22T00:00:00.000'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].description', 'Description of code 1'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].descriptionAr', ' 1 وصف الكود بالعربي'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].requestReason', 'Request reason text'

    -- Adds the "Authorization: Bearer <access_token>" header.
    EXEC sp_OASetProperty @http, 'AuthToken', '<access_token>'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://domain.com/api/v1.0/codetypes/requests/codes', '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>"
	-d '{
    "items": [
        {
            "codeType": "EGS",
            "parentCode": "10000051",
            "itemCode": "EG-113317713-5598542",
            "codeName": "Test Code",
            "codeNameAr": "كود تجريبي",
            "activeFrom": "2021-02-20T00:00:00.000",
            "activeTo": "2021-04-22T00:00:00.000",
            "description": "Description of code",
            "descriptionAr": "وصف الكود بالعربي",
            "requestReason": "Request reason text"
        },
        {
            "codeType": "EGS",
            "parentCode": "10000051",
            "itemCode": "EG-113317713-5598543",
            "codeName": "Test Code 1",
            "codeNameAr": "1 كود تجريبي",
            "activeFrom": "2021-02-20T00:00:00.000",
            "activeTo": "2021-04-22T00:00:00.000",
            "description": "Description of code 1",
            "descriptionAr": " 1 وصف الكود بالعربي",
            "requestReason": "Request reason text"
        }
    ]
}'
https://domain.com/api/v1.0/codetypes/requests/codes

Postman Collection Item JSON

{
  "name": "6. Create EGS Code Usage",
  "request": {
    "auth": {
      "type": "bearer",
      "bearer": [
        {
          "key": "token",
          "value": "{{generatedAccessToken}}",
          "type": "string"
        }
      ]
    },
    "method": "POST",
    "header": [
    ],
    "body": {
      "mode": "raw",
      "raw": "{\r\n    \"items\": [\r\n        {\r\n            \"codeType\": \"EGS\",\r\n            \"parentCode\": \"10000051\",\r\n            \"itemCode\": \"EG-113317713-5598542\",\r\n            \"codeName\": \"Test Code\",\r\n            \"codeNameAr\": \"كود تجريبي\",\r\n            \"activeFrom\": \"2021-02-20T00:00:00.000\",\r\n            \"activeTo\": \"2021-04-22T00:00:00.000\",\r\n            \"description\": \"Description of code\",\r\n            \"descriptionAr\": \"وصف الكود بالعربي\",\r\n            \"requestReason\": \"Request reason text\"\r\n        },\r\n        {\r\n            \"codeType\": \"EGS\",\r\n            \"parentCode\": \"10000051\",\r\n            \"itemCode\": \"EG-113317713-5598543\",\r\n            \"codeName\": \"Test Code 1\",\r\n            \"codeNameAr\": \"1 كود تجريبي\",\r\n            \"activeFrom\": \"2021-02-20T00:00:00.000\",\r\n            \"activeTo\": \"2021-04-22T00:00:00.000\",\r\n            \"description\": \"Description of code 1\",\r\n            \"descriptionAr\": \" 1 وصف الكود بالعربي\",\r\n            \"requestReason\": \"Request reason text\"\r\n        }\r\n    ]\r\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{apiBaseUrl}}/api/v1.0/codetypes/requests/codes",
      "host": [
        "{{apiBaseUrl}}"
      ],
      "path": [
        "api",
        "v1.0",
        "codetypes",
        "requests",
        "codes"
      ]
    }
  },
  "response": [
  ]
}