Chilkat Online Tools

SQL Server / Salesforce Platform APIs / Bulk Create Batch

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
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    DECLARE @bdRequestBody int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.BinData', @bdRequestBody OUT

    EXEC sp_OAMethod @bdRequestBody, 'LoadFile', @success OUT, 'file'
    IF @success <> 1
      BEGIN

        PRINT 'Failed to load file'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @bdRequestBody
        RETURN
      END

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'X-SFDC-Session', '{{_accessToken}}'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Accept-Encoding', 'gzip'
    -- Adds the "Authorization: Bearer <access_token>" header.
    EXEC sp_OASetProperty @http, 'AuthToken', '<access_token>'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'charset', 'UTF-8'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Encoding', 'gzip'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PBinaryBd', @resp OUT, 'POST', 'https://domain.com/services/async/{{version}}/job/{{_jobId}}/batch', @bdRequestBody, 'zip/csv', 0, 0
    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 @bdRequestBody
        RETURN
      END

    DECLARE @sbResponseBody int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody
    EXEC @hr = sp_OADestroy @resp

    DECLARE @xmlResponse int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Xml', @xmlResponse OUT

    EXEC sp_OAMethod @xmlResponse, 'LoadSb', @success OUT, @sbResponseBody, 1
    EXEC sp_OAMethod @xmlResponse, 'GetXml', @sTmp0 OUT
    PRINT @sTmp0

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

    -- <?xml version="1.0" encoding="UTF-8"?>
    -- <batchInfo xmlns="http://www.force.com/2009/06/asyncapi/dataload">
    --     <id>751...</id>
    --     <jobId>750...</jobId>
    --     <state>Queued</state>
    --     <createdDate>datetime</createdDate>
    --     <systemModstamp>datetime</systemModstamp>
    --     <numberRecordsProcessed>0</numberRecordsProcessed>
    --     <numberRecordsFailed>0</numberRecordsFailed>
    --     <totalProcessingTime>0</totalProcessingTime>
    --     <apiActiveProcessingTime>0</apiActiveProcessingTime>
    --     <apexProcessingTime>0</apexProcessingTime>
    -- </batchInfo>

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

    DECLARE @batchInfo_xmlns nvarchar(4000)
    EXEC sp_OAMethod @xmlResponse, 'GetAttrValue', @batchInfo_xmlns OUT, 'xmlns'
    DECLARE @id nvarchar(4000)
    EXEC sp_OAMethod @xmlResponse, 'GetChildContent', @id OUT, 'id'
    DECLARE @jobId nvarchar(4000)
    EXEC sp_OAMethod @xmlResponse, 'GetChildContent', @jobId OUT, 'jobId'
    DECLARE @state nvarchar(4000)
    EXEC sp_OAMethod @xmlResponse, 'GetChildContent', @state OUT, 'state'
    DECLARE @createdDate nvarchar(4000)
    EXEC sp_OAMethod @xmlResponse, 'GetChildContent', @createdDate OUT, 'createdDate'
    DECLARE @systemModstamp nvarchar(4000)
    EXEC sp_OAMethod @xmlResponse, 'GetChildContent', @systemModstamp OUT, 'systemModstamp'
    DECLARE @numberRecordsProcessed int
    EXEC sp_OAMethod @xmlResponse, 'GetChildIntValue', @numberRecordsProcessed OUT, 'numberRecordsProcessed'
    DECLARE @numberRecordsFailed int
    EXEC sp_OAMethod @xmlResponse, 'GetChildIntValue', @numberRecordsFailed OUT, 'numberRecordsFailed'
    DECLARE @totalProcessingTime int
    EXEC sp_OAMethod @xmlResponse, 'GetChildIntValue', @totalProcessingTime OUT, 'totalProcessingTime'
    DECLARE @apiActiveProcessingTime int
    EXEC sp_OAMethod @xmlResponse, 'GetChildIntValue', @apiActiveProcessingTime OUT, 'apiActiveProcessingTime'
    DECLARE @apexProcessingTime int
    EXEC sp_OAMethod @xmlResponse, 'GetChildIntValue', @apexProcessingTime OUT, 'apexProcessingTime'

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @bdRequestBody
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @xmlResponse


END
GO

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	-H "X-SFDC-Session: {{_accessToken}}"
	-H "Content-Type: text/csv"
	-H "Content-Type: zip/csv"
	-H "charset: UTF-8"
	-H "Accept-Encoding: gzip"
	-H "Content-Encoding: gzip"
	--data-binary '@file'
https://domain.com/services/async/{{version}}/job/{{_jobId}}/batch

Postman Collection Item JSON

{
  "name": "Bulk Create Batch",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "const context = pm.environment.name ? pm.environment : pm.collectionVariables;",
          "",
          "var xmlTree = xml2Json(pm.response.text());",
          "console.log(xmlTree);",
          "context.set(\"_batchId\", xmlTree['batchInfo']['id']);"
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "X-SFDC-Session",
        "value": "{{_accessToken}}"
      },
      {
        "key": "Content-Type",
        "value": "text/csv"
      },
      {
        "key": "Content-Type",
        "value": "zip/csv",
        "disabled": true
      },
      {
        "key": "charset",
        "value": "UTF-8"
      },
      {
        "key": "Accept-Encoding",
        "value": "gzip",
        "disabled": true
      },
      {
        "key": "Content-Encoding",
        "value": "gzip",
        "disabled": true
      }
    ],
    "body": {
      "mode": "file",
      "file": {
        "src": ""
      }
    },
    "url": {
      "raw": "{{_endpoint}}/services/async/{{version}}/job/{{_jobId}}/batch",
      "host": [
        "{{_endpoint}}"
      ],
      "path": [
        "services",
        "async",
        "{{version}}",
        "job",
        "{{_jobId}}",
        "batch"
      ]
    }
  },
  "response": [
    {
      "name": "Successful Bulk Create Batch",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "X-SFDC-Session",
            "value": "{{_accessToken}}"
          },
          {
            "key": "Content-Type",
            "value": "text/csv"
          },
          {
            "key": "Content-Type",
            "value": "zip/csv",
            "disabled": true
          },
          {
            "key": "charset",
            "value": "UTF-8"
          },
          {
            "key": "Accept-Encoding",
            "value": "gzip",
            "disabled": true
          },
          {
            "key": "Content-Encoding",
            "value": "gzip",
            "disabled": true
          }
        ],
        "body": {
          "mode": "file",
          "file": {
            "src": "/Users/username/Downloads/account.csv"
          }
        },
        "url": {
          "raw": "{{_endpoint}}/services/async/{{version}}/job/{{_jobId}}/batch",
          "host": [
            "{{_endpoint}}"
          ],
          "path": [
            "services",
            "async",
            "{{version}}",
            "job",
            "{{_jobId}}",
            "batch"
          ]
        }
      },
      "status": "Created",
      "code": 201,
      "_postman_previewlanguage": "xml",
      "header": [
        {
          "key": "Date",
          "value": "Thu, 16 Nov 2023 16:16:10 GMT"
        },
        {
          "key": "Strict-Transport-Security",
          "value": "max-age=63072000; includeSubDomains"
        },
        {
          "key": "X-Content-Type-Options",
          "value": "nosniff"
        },
        {
          "key": "X-XSS-Protection",
          "value": "1; mode=block"
        },
        {
          "key": "X-Robots-Tag",
          "value": "none"
        },
        {
          "key": "Cache-Control",
          "value": "no-cache,must-revalidate,max-age=0,no-store,private"
        },
        {
          "key": "Location",
          "value": "/services/async/58.0/job/7502o00000kjNCsAAM/batch/7512o00001DSJWnAAP"
        },
        {
          "key": "Content-Type",
          "value": "application/xml"
        },
        {
          "key": "Content-Encoding",
          "value": "gzip"
        },
        {
          "key": "Transfer-Encoding",
          "value": "chunked"
        }
      ],
      "cookie": [
      ],
      "body": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<batchInfo\n   xmlns=\"http://www.force.com/2009/06/asyncapi/dataload\">\n    <id>751...</id>\n    <jobId>750...</jobId>\n    <state>Queued</state>\n    <createdDate>datetime</createdDate>\n    <systemModstamp>datetime</systemModstamp>\n    <numberRecordsProcessed>0</numberRecordsProcessed>\n    <numberRecordsFailed>0</numberRecordsFailed>\n    <totalProcessingTime>0</totalProcessingTime>\n    <apiActiveProcessingTime>0</apiActiveProcessingTime>\n    <apexProcessingTime>0</apexProcessingTime>\n</batchInfo>"
    }
  ]
}