Chilkat Online Tools

Swift3 / Zoho CRM REST APIs / Create Bulk Write Job

Back to Collection Items

func chilkatTest() {
    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    let http = CkoHttp()!
    var success: Bool

    // Use this online tool to generate code from sample JSON: Generate Code to Create JSON

    // The following JSON is sent in the request body.

    // {
    //   "operation": "insert",
    //   "ignore_empty": true,
    //   "callback": {
    //     "url": "https://www.zoho.com/in/crm/",
    //     "method": "post"
    //   },
    //   "resource": [
    //     {
    //       "type": "data",
    //       "module": "Leads",
    //       "file_id": "{{file_id}}",
    //       "field_mappings": [
    //         {
    //           "api_name": "Last_Name",
    //           "index": 0
    //         },
    //         {
    //           "api_name": "Owner",
    //           "index": 1
    //         },
    //         {
    //           "api_name": "Date_3",
    //           "index": 2,
    //           "format": "yyyy/MM/dd"
    //         },
    //         {
    //           "api_name": "Mobile",
    //           "index": 3
    //         },
    //         {
    //           "api_name": "Layout",
    //           "index": 4
    //         }
    //       ]
    //     }
    //   ]
    // }

    let json = CkoJsonObject()!
    json.update("operation", value: "insert")
    json.updateBool("ignore_empty", value: true)
    json.update("callback.url", value: "https://www.zoho.com/in/crm/")
    json.update("callback.method", value: "post")
    json.update("resource[0].type", value: "data")
    json.update("resource[0].module", value: "Leads")
    json.update("resource[0].file_id", value: "{{file_id}}")
    json.update("resource[0].field_mappings[0].api_name", value: "Last_Name")
    json.updateInt("resource[0].field_mappings[0].index", value: 0)
    json.update("resource[0].field_mappings[1].api_name", value: "Owner")
    json.updateInt("resource[0].field_mappings[1].index", value: 1)
    json.update("resource[0].field_mappings[2].api_name", value: "Date_3")
    json.updateInt("resource[0].field_mappings[2].index", value: 2)
    json.update("resource[0].field_mappings[2].format", value: "yyyy/MM/dd")
    json.update("resource[0].field_mappings[3].api_name", value: "Mobile")
    json.updateInt("resource[0].field_mappings[3].index", value: 3)
    json.update("resource[0].field_mappings[4].api_name", value: "Layout")
    json.updateInt("resource[0].field_mappings[4].index", value: 4)

    // Adds the "Authorization: Bearer <access_token>" header.
    http.authToken = "<access_token>"

    var resp: CkoHttpResponse? = http.postJson3("https://domain.com/crm/bulk/v2/write", contentType: "application/json", json: json)
    if http.lastMethodSuccess == false {
        print("\(http.lastErrorText!)")
        return
    }

    let sbResponseBody = CkoStringBuilder()!
    resp!.getBodySb(sbResponseBody)

    let jResp = CkoJsonObject()!
    jResp.loadSb(sbResponseBody)
    jResp.emitCompact = false

    print("Response Body:")
    print("\(jResp.emit()!)")

    var respStatusCode: Int = resp!.statusCode.intValue
    print("Response Status Code = \(respStatusCode)")
    if respStatusCode >= 400 {
        print("Response Header:")
        print("\(resp!.header!)")
        print("Failed.")
        resp = nil
        return
    }

    resp = nil

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

    // {
    //   "status": "success",
    //   "code": "SUCCESS",
    //   "message": "success",
    //   "details": {
    //     "id": "4150868000005392002",
    //     "created_by": {
    //       "id": "4150868000000225013",
    //       "name": "Patricia Boyle"
    //     }
    //   }
    // }

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

    var status: String? = jResp.string(of: "status")
    var code: String? = jResp.string(of: "code")
    var message: String? = jResp.string(of: "message")
    var Id: String? = jResp.string(of: "details.id")
    var Created_byId: String? = jResp.string(of: "details.created_by.id")
    var Name: String? = jResp.string(of: "details.created_by.name")

}

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	-d '{
  "operation": "insert",
  "ignore_empty": true,
  "callback": {
    "url": "https://www.zoho.com/in/crm/",
    "method": "post"
  },
  "resource": [
    {
      "type": "data",
      "module": "Leads",
      "file_id": "{{file_id}}",
      "field_mappings": [
        {
          "api_name": "Last_Name",
          "index": 0
        },
        {
          "api_name": "Owner",
          "index": 1
        },
        {
          "api_name": "Date_3",
          "index": 2,
          "format": "yyyy/MM/dd"
        },
        {
          "api_name": "Mobile",
          "index": 3
        },
        {
          "api_name": "Layout",
          "index": 4
        }
      ]
    }
  ]
}'
https://domain.com/crm/bulk/v2/write

Postman Collection Item JSON

{
  "name": "Create Bulk Write Job",
  "request": {
    "auth": {
      "type": "bearer",
      "bearer": [
        {
          "key": "token",
          "value": "{{access-token}}",
          "type": "string"
        }
      ]
    },
    "method": "POST",
    "header": [
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n  \"operation\": \"insert\",\n  \"ignore_empty\": true,\n  \"callback\": {\n    \"url\": \"https://www.zoho.com/in/crm/\",\n    \"method\": \"post\"\n  },\n  \"resource\": [\n    {\n      \"type\": \"data\",\n      \"module\": \"Leads\",\n      \"file_id\": \"{{file_id}}\",\n      \"field_mappings\": [\n        {\n          \"api_name\": \"Last_Name\",\n          \"index\": 0\n        },\n        {\n          \"api_name\": \"Owner\",\n          \"index\": 1\n        },\n        {\n          \"api_name\": \"Date_3\",\n          \"index\": 2,\n          \"format\": \"yyyy/MM/dd\"\n        },\n        {\n          \"api_name\": \"Mobile\",\n          \"index\": 3\n        },\n        {\n          \"api_name\": \"Layout\",\n          \"index\": 4\n        }\n      ]\n    }\n  ]\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{api-domain}}/crm/bulk/v2/write",
      "host": [
        "{{api-domain}}"
      ],
      "path": [
        "crm",
        "bulk",
        "v2",
        "write"
      ]
    },
    "description": "To create a bulk write job to import records into CRM."
  },
  "response": [
    {
      "name": "SUCCESS RESPONSE",
      "originalRequest": {
        "method": "POST",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"operation\": \"insert\",\n  \"ignore_empty\": true,\n  \"callback\": {\n    \"url\": \"https://www.zoho.com/in/crm/\",\n    \"method\": \"post\"\n  },\n  \"resource\": [\n    {\n      \"type\": \"data\",\n      \"module\": \"Leads\",\n      \"file_id\": \"4150868000005379001\",\n      \"field_mappings\": [\n        {\n          \"api_name\": \"Last_Name\",\n          \"index\": 0\n        },\n        {\n          \"api_name\": \"Owner\",\n          \"index\": 1\n        },\n        {\n          \"api_name\": \"Date_3\",\n          \"index\": 2,\n          \"format\": \"yyyy/MM/dd\"\n        },\n        {\n          \"api_name\": \"Mobile\",\n          \"index\": 3\n        },\n        {\n          \"api_name\": \"Layout\",\n          \"index\": 4\n        }\n      ]\n    }\n  ]\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{api-domain}}/crm/bulk/v2/write",
          "host": [
            "{{api-domain}}"
          ],
          "path": [
            "crm",
            "bulk",
            "v2",
            "write"
          ]
        }
      },
      "status": "Created",
      "code": 201,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "ZGS"
        },
        {
          "key": "Date",
          "value": "Thu, 13 May 2021 05:30:58 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json"
        },
        {
          "key": "Transfer-Encoding",
          "value": "chunked"
        },
        {
          "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-ACCESSTOKEN-RESET",
          "value": "2021-05-13T12:00:55+05:30"
        },
        {
          "key": "vary",
          "value": "accept-encoding"
        },
        {
          "key": "Content-Encoding",
          "value": "gzip"
        },
        {
          "key": "Strict-Transport-Security",
          "value": "max-age=63072000"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"status\": \"success\",\n    \"code\": \"SUCCESS\",\n    \"message\": \"success\",\n    \"details\": {\n        \"id\": \"4150868000005392002\",\n        \"created_by\": {\n            \"id\": \"4150868000000225013\",\n            \"name\": \"Patricia Boyle\"\n        }\n    }\n}"
    },
    {
      "name": "MISSING_REQUIRED_KEY",
      "originalRequest": {
        "method": "POST",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"operation\": \"insert\",\n    \"ignore_empty\": true,\n    \"callback\": {\n        \"url\": \"https://www.zoho.com/in/crm/\",\n        \"method\": \"post\"\n    },\n    \"resource\": [\n        {\n            \"type\": \"data\",\n            \"module\": \"Leads\",\n            \"field_mappings\": [\n                {\n                    \"api_name\": \"Last_Name\",\n                    \"index\": 0\n                },\n                {\n                    \"api_name\": \"Owner\",\n                    \"index\": 1\n                },\n                {\n                    \"api_name\": \"Date_3\",\n                    \"index\": 2,\n                    \"format\": \"yyyy/MM/dd\"\n                },\n                {\n                    \"api_name\": \"Mobile\",\n                    \"index\": 3\n                },\n                {\n                    \"api_name\": \"Layout\",\n                    \"index\": 4\n                }\n            ]\n        }\n    ]\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{api-domain}}/crm/bulk/v2/write",
          "host": [
            "{{api-domain}}"
          ],
          "path": [
            "crm",
            "bulk",
            "v2",
            "write"
          ]
        }
      },
      "status": "Bad Request",
      "code": 400,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "ZGS"
        },
        {
          "key": "Date",
          "value": "Thu, 13 May 2021 05:31:15 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json"
        },
        {
          "key": "Transfer-Encoding",
          "value": "chunked"
        },
        {
          "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-ACCESSTOKEN-RESET",
          "value": "2021-05-13T12:00:55+05:30"
        },
        {
          "key": "vary",
          "value": "accept-encoding"
        },
        {
          "key": "Content-Encoding",
          "value": "gzip"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"status\": \"error\",\n    \"code\": \"MISSING_REQUIRED_KEY\",\n    \"message\": \"required key fileid is not found in request body.\",\n    \"details\": {}\n}"
    },
    {
      "name": "INVALID_CALLBACK_URL",
      "originalRequest": {
        "method": "POST",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"operation\": \"insert\",\n  \"ignore_empty\": true,\n  \"callback\": {\n    \"url\": \"http://requestbin.fullcontact.com\",\n    \"method\": \"post\"\n  },\n  \"resource\": [\n    {\n      \"type\": \"data\",\n      \"module\": \"Leads\",\n      \"file_id\": \"4150868000005379001\",\n      \"field_mappings\": [\n        {\n          \"api_name\": \"Last_Name\",\n          \"index\": 0\n        },\n        {\n          \"api_name\": \"Owner\",\n          \"index\": 1\n        },\n        {\n          \"api_name\": \"Date_3\",\n          \"index\": 2,\n          \"format\": \"yyyy/MM/dd\"\n        },\n        {\n          \"api_name\": \"Mobile\",\n          \"index\": 3\n        },\n        {\n          \"api_name\": \"Layout\",\n          \"index\": 4\n        }\n      ]\n    }\n  ]\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{api-domain}}/crm/bulk/v2/write",
          "host": [
            "{{api-domain}}"
          ],
          "path": [
            "crm",
            "bulk",
            "v2",
            "write"
          ]
        }
      },
      "status": "Bad Request",
      "code": 400,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "ZGS"
        },
        {
          "key": "Date",
          "value": "Thu, 13 May 2021 05:31:50 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json"
        },
        {
          "key": "Transfer-Encoding",
          "value": "chunked"
        },
        {
          "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-ACCESSTOKEN-RESET",
          "value": "2021-05-13T12:00:55+05:30"
        },
        {
          "key": "vary",
          "value": "accept-encoding"
        },
        {
          "key": "Content-Encoding",
          "value": "gzip"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"status\": \"error\",\n    \"code\": \"INVALID_CALLBACK_URL\",\n    \"message\": \"Invalid URL 'http://requestbin.fullcontact.com' for callback.\",\n    \"details\": {\n        \"reason\": \"possible ssrf attempt\"\n    }\n}"
    },
    {
      "name": "FILE_NOT_SUPPORTED",
      "originalRequest": {
        "method": "POST",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"operation\": \"insert\",\n  \"ignore_empty\": true,\n  \"callback\": {\n    \"url\": \"https://www.zoho.com/in/crm/\",\n    \"method\": \"post\"\n  },\n  \"resource\": [\n    {\n      \"type\": \"data\",\n      \"module\": \"Leads\",\n      \"file_id\": \"415086800000537900\",\n      \"field_mappings\": [\n        {\n          \"api_name\": \"Last_Name\",\n          \"index\": 0\n        },\n        {\n          \"api_name\": \"Owner\",\n          \"index\": 1\n        },\n        {\n          \"api_name\": \"Date_3\",\n          \"index\": 2,\n          \"format\": \"yyyy/MM/dd\"\n        },\n        {\n          \"api_name\": \"Mobile\",\n          \"index\": 3\n        },\n        {\n          \"api_name\": \"Layout\",\n          \"index\": 4\n        }\n      ]\n    }\n  ]\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{api-domain}}/crm/bulk/v2/write",
          "host": [
            "{{api-domain}}"
          ],
          "path": [
            "crm",
            "bulk",
            "v2",
            "write"
          ]
        }
      },
      "status": "Bad Request",
      "code": 400,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "ZGS"
        },
        {
          "key": "Date",
          "value": "Thu, 13 May 2021 05:32:27 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json"
        },
        {
          "key": "Transfer-Encoding",
          "value": "chunked"
        },
        {
          "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-ACCESSTOKEN-RESET",
          "value": "2021-05-13T12:00:55+05:30"
        },
        {
          "key": "vary",
          "value": "accept-encoding"
        },
        {
          "key": "Content-Encoding",
          "value": "gzip"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"status\": \"error\",\n    \"code\": \"FILE_NOT_SUPPORTED\",\n    \"message\": \"File not supported for bulk write\",\n    \"details\": {}\n}"
    }
  ]
}