Chilkat Online Tools

Android / Zoho CRM REST APIs / Add User

Back to Collection Items

// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

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

    CkHttp http = new CkHttp();
    boolean success;

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

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

    // {
    //   "users": [
    //     {
    //       "role": "{{role_id}}",
    //       "first_name": "Patricia",
    //       "email": "Patricia@abcl.com",
    //       "profile": "{{profile_id}}",
    //       "last_name": "Boyle",
    //       "Currency": "AFN",
    //       "territories": [
    //         {
    //           "manager": true,
    //           "name": "North Zone",
    //           "id": "{{territory_id}}"
    //         },
    //         {
    //           "manager": true,
    //           "name": "South Zone",
    //           "id": "{{territory_id}}"
    //         }
    //       ]
    //     }
    //   ]
    // }

    CkJsonObject json = new CkJsonObject();
    json.UpdateString("users[0].role","{{role_id}}");
    json.UpdateString("users[0].first_name","Patricia");
    json.UpdateString("users[0].email","Patricia@abcl.com");
    json.UpdateString("users[0].profile","{{profile_id}}");
    json.UpdateString("users[0].last_name","Boyle");
    json.UpdateString("users[0].Currency","AFN");
    json.UpdateBool("users[0].territories[0].manager",true);
    json.UpdateString("users[0].territories[0].name","North Zone");
    json.UpdateString("users[0].territories[0].id","{{territory_id}}");
    json.UpdateBool("users[0].territories[1].manager",true);
    json.UpdateString("users[0].territories[1].name","South Zone");
    json.UpdateString("users[0].territories[1].id","{{territory_id}}");

    // Adds the "Authorization: Bearer <access_token>" header.
    http.put_AuthToken("<access_token>");
    http.SetRequestHeader("Content-Type","application/json");

    CkHttpResponse resp = http.PostJson3("https://domain.com/crm/v2/users","application/json",json);
    if (http.get_LastMethodSuccess() == false) {
        Log.i(TAG, http.lastErrorText());
        return;
        }

    CkStringBuilder sbResponseBody = new CkStringBuilder();
    resp.GetBodySb(sbResponseBody);

    CkJsonObject jResp = new CkJsonObject();
    jResp.LoadSb(sbResponseBody);
    jResp.put_EmitCompact(false);

    Log.i(TAG, "Response Body:");
    Log.i(TAG, jResp.emit());

    int respStatusCode = resp.get_StatusCode();
    Log.i(TAG, "Response Status Code = " + String.valueOf(respStatusCode));
    if (respStatusCode >= 400) {
        Log.i(TAG, "Response Header:");
        Log.i(TAG, resp.header());
        Log.i(TAG, "Failed.");

        return;
        }

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

    // {
    //   "users": [
    //     {
    //       "code": "SUCCESS",
    //       "details": {
    //         "id": "738964000002148003"
    //       },
    //       "message": "User added",
    //       "status": "success"
    //     }
    //   ]
    // }

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

    String code;
    String Id;
    String message;
    String status;

    int i = 0;
    int count_i = jResp.SizeOfArray("users");
    while (i < count_i) {
        jResp.put_I(i);
        code = jResp.stringOf("users[i].code");
        Id = jResp.stringOf("users[i].details.id");
        message = jResp.stringOf("users[i].message");
        status = jResp.stringOf("users[i].status");
        i = i + 1;
        }


  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	-H "Content-Type: application/json"
	-d '{
    "users": [
        {
            "role": "{{role_id}}",
            "first_name": "Patricia",
            "email": "Patricia@abcl.com",
            "profile": "{{profile_id}}",
            "last_name": "Boyle",
            "Currency": "AFN",
            "territories": [
                {
                    "manager": true,
                    "name": "North Zone",
                    "id": "{{territory_id}}"
                },
                {
                    "manager": true,
                    "name": "South Zone",
                    "id": "{{territory_id}}"
                }
            ]
        }
    ]
}'
https://domain.com/crm/v2/users

Postman Collection Item JSON

{
  "name": "Add User",
  "request": {
    "auth": {
      "type": "bearer",
      "bearer": [
        {
          "key": "token",
          "value": "{{access-token}}",
          "type": "string"
        }
      ]
    },
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "name": "Content-Type",
        "value": "application/json",
        "type": "text"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"users\": [\n        {\n            \"role\": \"{{role_id}}\",\n            \"first_name\": \"Patricia\",\n            \"email\": \"Patricia@abcl.com\",\n            \"profile\": \"{{profile_id}}\",\n            \"last_name\": \"Boyle\",\n            \"Currency\": \"AFN\",\n            \"territories\": [\n                {\n                    \"manager\": true,\n                    \"name\": \"North Zone\",\n                    \"id\": \"{{territory_id}}\"\n                },\n                {\n                    \"manager\": true,\n                    \"name\": \"South Zone\",\n                    \"id\": \"{{territory_id}}\"\n                }\n            ]\n        }\n    ]\n}"
    },
    "url": {
      "raw": "{{api-domain}}/crm/v2/users",
      "host": [
        "{{api-domain}}"
      ],
      "path": [
        "crm",
        "v2",
        "users"
      ]
    },
    "description": "To add a user to your organization."
  },
  "response": [
    {
      "name": "SUCCESS RESPONSE",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "name": "Content-Type",
            "value": "application/json",
            "type": "text"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"users\": [\n    {\n      \"role\": \"738964000000015966\",\n      \"first_name\": \"Patricia\",\n      \"email\": \"Patricia@abcl2.com\",\n      \"profile\": \"738964000000015972\",\n      \"last_name\": \"Boyle\"\n    }\n  ]\n}"
        },
        "url": {
          "raw": "{{api-domain}}/crm/v2/users",
          "host": [
            "{{api-domain}}"
          ],
          "path": [
            "crm",
            "v2",
            "users"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "ZGS"
        },
        {
          "key": "Date",
          "value": "Tue, 11 May 2021 11:57:41 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json;charset=utf-8"
        },
        {
          "key": "Content-Length",
          "value": "110"
        },
        {
          "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    \"users\": [\n        {\n            \"code\": \"SUCCESS\",\n            \"details\": {\n                \"id\": \"738964000002148003\"\n            },\n            \"message\": \"User added\",\n            \"status\": \"success\"\n        }\n    ]\n}"
    },
    {
      "name": "DUPLICATE_DATA",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "name": "Content-Type",
            "value": "application/json",
            "type": "text"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"users\": [\n    {\n      \"role\": \"738964000000015966\",\n      \"first_name\": \"Patricia\",\n      \"email\": \"Patricia@abcl2.com\",\n      \"profile\": \"738964000000015972\",\n      \"last_name\": \"Boyle\"\n    }\n  ]\n}"
        },
        "url": {
          "raw": "{{api-domain}}/crm/v2/users",
          "host": [
            "{{api-domain}}"
          ],
          "path": [
            "crm",
            "v2",
            "users"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "ZGS"
        },
        {
          "key": "Date",
          "value": "Tue, 11 May 2021 11:58:01 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json;charset=utf-8"
        },
        {
          "key": "Content-Length",
          "value": "191"
        },
        {
          "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    \"users\": [\n        {\n            \"code\": \"DUPLICATE_DATA\",\n            \"details\": {\n                \"id\": \"738964000002148003\",\n                \"email\": \"Patricia@abcl2.com\"\n            },\n            \"message\": \"Failed to add user since same email id is already present\",\n            \"status\": \"error\"\n        }\n    ]\n}"
    },
    {
      "name": "MANDATORY_NOT_FOUND",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "name": "Content-Type",
            "value": "application/json",
            "type": "text"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"users\": [\n        {\n            \"role\": \"738964000000015966\",\n            \"first_name\": \"Patricia\",\n            \"email\": \"Patricia@abcl2.com\",\n            \"profile\": \"738964000000015972\"\n        }\n    ]\n}"
        },
        "url": {
          "raw": "{{api-domain}}/crm/v2/users",
          "host": [
            "{{api-domain}}"
          ],
          "path": [
            "crm",
            "v2",
            "users"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "ZGS"
        },
        {
          "key": "Date",
          "value": "Tue, 11 May 2021 11:58:33 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": "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    \"users\": [\n        {\n            \"code\": \"MANDATORY_NOT_FOUND\",\n            \"details\": {\n                \"api_name\": \"last_name\"\n            },\n            \"message\": \"required field not found\",\n            \"status\": \"error\"\n        }\n    ]\n}"
    },
    {
      "name": "INVALID_DATA",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "name": "Content-Type",
            "value": "application/json",
            "type": "text"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"users\": [\n        {\n            \"role\": \"738964000000015966\",\n            \"first_name\": \"Patricia\",\n            \"email\": \"Patricia@abcl2.com\",\n            \"profile\": \"738964000000015972\",\n            \"last_name\": 123\n        }\n    ]\n}"
        },
        "url": {
          "raw": "{{api-domain}}/crm/v2/users",
          "host": [
            "{{api-domain}}"
          ],
          "path": [
            "crm",
            "v2",
            "users"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "ZGS"
        },
        {
          "key": "Date",
          "value": "Tue, 11 May 2021 11:58:54 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json;charset=utf-8"
        },
        {
          "key": "Content-Length",
          "value": "140"
        },
        {
          "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    \"users\": [\n        {\n            \"code\": \"INVALID_DATA\",\n            \"details\": {\n                \"expected_data_type\": \"text\",\n                \"api_name\": \"last_name\"\n            },\n            \"message\": \"invalid data\",\n            \"status\": \"error\"\n        }\n    ]\n}"
    },
    {
      "name": "INVALID_REQUEST_METHOD",
      "originalRequest": {
        "method": "COPY",
        "header": [
          {
            "key": "Content-Type",
            "name": "Content-Type",
            "value": "application/json",
            "type": "text"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"users\": [\n    {\n      \"role\": \"738964000000015966\",\n      \"first_name\": \"Patricia\",\n      \"email\": \"Patricia@abcl.com\",\n      \"profile\": \"738964000000015972\",\n      \"last_name\": \"Boyle\"\n    }\n  ]\n}"
        },
        "url": {
          "raw": "{{api-domain}}/crm/v2/users",
          "host": [
            "{{api-domain}}"
          ],
          "path": [
            "crm",
            "v2",
            "users"
          ]
        }
      },
      "status": "Bad Request",
      "code": 400,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "ZGS"
        },
        {
          "key": "Date",
          "value": "Tue, 11 May 2021 12:00:03 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": "POST",
        "header": [
          {
            "key": "Content-Type",
            "name": "Content-Type",
            "value": "application/json",
            "type": "text"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"users\": [\n    {\n      \"role\": \"738964000000015966\",\n      \"first_name\": \"Patricia\",\n      \"email\": \"Patricia@abcl.com\",\n      \"profile\": \"738964000000015972\",\n      \"last_name\": \"Boyle\"\n    }\n  ]\n}"
        },
        "url": {
          "raw": "{{api-domain}}/crm/v2/users",
          "host": [
            "{{api-domain}}"
          ],
          "path": [
            "crm",
            "v2",
            "users"
          ]
        }
      },
      "status": "Unauthorized",
      "code": 401,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "ZGS"
        },
        {
          "key": "Date",
          "value": "Tue, 11 May 2021 12:00:20 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": "POST",
        "header": [
          {
            "key": "Content-Type",
            "name": "Content-Type",
            "value": "application/json",
            "type": "text"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"users\": [\n        {\n            \"role\": \"{{role_id}}\",\n            \"first_name\": \"Patricia\",\n            \"email\": \"Patricia@abcl.com\",\n            \"profile\": \"{{profile_id}}\",\n            \"last_name\": \"Boyle\",\n            \"Currency\": \"AFN\",\n            \"territories\": [\n                {\n                    \"manager\": true,\n                    \"name\": \"North Zone\",\n                    \"id\": \"{{territory_id}}\"\n                },\n                {\n                    \"manager\": true,\n                    \"name\": \"South Zone\",\n                    \"id\": \"{{territory_id}}\"\n                }\n            ]\n        }\n    ]\n}"
        },
        "url": {
          "raw": "{{api-domain}}/crm/v2/users",
          "host": [
            "{{api-domain}}"
          ],
          "path": [
            "crm",
            "v2",
            "users"
          ]
        }
      },
      "status": "Unauthorized",
      "code": 401,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "ZGS"
        },
        {
          "key": "Date",
          "value": "Thu, 13 May 2021 06:32:35 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}"
    }
  ]
}