Chilkat Online Tools

Node.js / Zoho CRM REST APIs / Update Role

Back to Collection Items

var os = require('os');
if (os.platform() == 'win32') {  
    if (os.arch() == 'ia32') {
        var chilkat = require('@chilkat/ck-node21-win-ia32');
    } else {
        var chilkat = require('@chilkat/ck-node21-win64'); 
    }
} else if (os.platform() == 'linux') {
    if (os.arch() == 'arm') {
        var chilkat = require('@chilkat/ck-node21-arm');
    } else if (os.arch() == 'x86') {
        var chilkat = require('@chilkat/ck-node21-linux32');
    } else {
        var chilkat = require('@chilkat/ck-node21-linux64');
    }
} else if (os.platform() == 'darwin') {
    if (os.arch() == 'arm64') {
        var chilkat = require('@chilkat/ck-node21-mac-m1');
    } else {
        var chilkat = require('@chilkat/ck-node21-macosx');
    }
}


function chilkatExample() {

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

    var http = new chilkat.Http();
    var 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.

    // {
    //   "roles": [
    //     {
    //       "id": "738964000000015969",
    //       "name": "Sales department Head",
    //       "reporting_to": "738964000000015966",
    //       "forecast_manager": "738964000000291009",
    //       "description": "Manage the sales department",
    //       "share_with_peers": true
    //     }
    //   ]
    // }

    var json = new chilkat.JsonObject();
    json.UpdateString("roles[0].id","738964000000015969");
    json.UpdateString("roles[0].name","Sales department Head");
    json.UpdateString("roles[0].reporting_to","738964000000015966");
    json.UpdateString("roles[0].forecast_manager","738964000000291009");
    json.UpdateString("roles[0].description","Manage the sales department");
    json.UpdateBool("roles[0].share_with_peers",true);

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

    var sbRequestBody = new chilkat.StringBuilder();
    json.EmitSb(sbRequestBody);

    // resp: HttpResponse
    var resp = http.PTextSb("PUT","https://domain.com/crm/v2.1/settings/roles",sbRequestBody,"utf-8","application/json",false,false);
    if (http.LastMethodSuccess == false) {
        console.log(http.LastErrorText);
        return;
    }

    var sbResponseBody = new chilkat.StringBuilder();
    resp.GetBodySb(sbResponseBody);

    var jResp = new chilkat.JsonObject();
    jResp.LoadSb(sbResponseBody);
    jResp.EmitCompact = false;

    console.log("Response Body:");
    console.log(jResp.Emit());

    var respStatusCode = resp.StatusCode;
    console.log("Response Status Code = " + respStatusCode);
    if (respStatusCode >= 400) {
        console.log("Response Header:");
        console.log(resp.Header);
        console.log("Failed.");

        return;
    }

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

    // {
    //   "roles": [
    //     {
    //       "code": "SUCCESS",
    //       "details": {
    //         "id": "738964000000015969"
    //       },
    //       "message": "Role updated",
    //       "status": "success"
    //     }
    //   ]
    // }

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

    var code;
    var Id;
    var message;
    var status;

    var i = 0;
    var count_i = jResp.SizeOfArray("roles");
    while (i < count_i) {
        jResp.I = i;
        code = jResp.StringOf("roles[i].code");
        Id = jResp.StringOf("roles[i].details.id");
        message = jResp.StringOf("roles[i].message");
        status = jResp.StringOf("roles[i].status");
        i = i+1;
    }


}

chilkatExample();

Curl Command

curl -X PUT
	-H "Authorization: Bearer <access_token>"
	-d '{
    "roles": [
        {
            "id": "738964000000015969",
            "name": "Sales department Head",
            "reporting_to": "738964000000015966",
            "forecast_manager": "738964000000291009",
            "description": "Manage the sales department",
            "share_with_peers": true
        }
    ]
}'
https://domain.com/crm/v2.1/settings/roles

Postman Collection Item JSON

{
  "name": "Update Role",
  "request": {
    "auth": {
      "type": "bearer",
      "bearer": [
        {
          "key": "token",
          "value": "{{access-token}}",
          "type": "string"
        }
      ]
    },
    "method": "PUT",
    "header": [
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"roles\": [\n        {\n            \"id\": \"738964000000015969\",\n            \"name\": \"Sales department Head\",\n            \"reporting_to\": \"738964000000015966\",\n            \"forecast_manager\": \"738964000000291009\",\n            \"description\": \"Manage the sales department\",\n            \"share_with_peers\": true\n        }\n    ]\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{api-domain}}/crm/v2.1/settings/roles",
      "host": [
        "{{api-domain}}"
      ],
      "path": [
        "crm",
        "v2.1",
        "settings",
        "roles"
      ]
    },
    "description": "To update a role in your organization."
  },
  "response": [
    {
      "name": "SUCCESS RESPONSE",
      "originalRequest": {
        "method": "PUT",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"roles\": [\n        {\n            \"id\": \"738964000000015969\",\n            \"name\": \"Sales department Head\",\n            \"reporting_to\": \"738964000000015966\",\n            \"forecast_manager\": \"738964000002006002\",\n            \"description\": \"Manage the sales department\",\n            \"share_with_peers\": true\n        }\n    ]\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{api-domain}}/crm/v2.1/settings/roles",
          "host": [
            "{{api-domain}}"
          ],
          "path": [
            "crm",
            "v2.1",
            "settings",
            "roles"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "ZGS"
        },
        {
          "key": "Date",
          "value": "Tue, 15 Jun 2021 10:25:45 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json;charset=utf-8"
        },
        {
          "key": "Content-Length",
          "value": "112"
        },
        {
          "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-06-15T11:15:35+00:00"
        },
        {
          "key": "clientVersion",
          "value": "4116249"
        },
        {
          "key": "clientsubVersion",
          "value": "0a372498832d823bc03c7945101b2b5e"
        },
        {
          "key": "Content-Disposition",
          "value": "attachment; filename=response.json"
        },
        {
          "key": "Content-Language",
          "value": "en-US"
        },
        {
          "key": "Strict-Transport-Security",
          "value": "max-age=63072000"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"roles\": [\n        {\n            \"code\": \"SUCCESS\",\n            \"details\": {\n                \"id\": \"738964000000015969\"\n            },\n            \"message\": \"Role updated\",\n            \"status\": \"success\"\n        }\n    ]\n}"
    },
    {
      "name": "INVALID_DATA (Forecast Manager ID)",
      "originalRequest": {
        "method": "PUT",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"roles\": [\n        {\n            \"id\": \"738964000000015969\",\n            \"name\": \"Sales department Head\",\n            \"reporting_to\": \"738964000000015966\",\n            \"forecast_manager\": \"738964000000291009\",\n            \"description\": \"Manage the sales department\",\n            \"share_with_peers\": true\n        }\n    ]\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{api-domain}}/crm/v2.1/settings/roles",
          "host": [
            "{{api-domain}}"
          ],
          "path": [
            "crm",
            "v2.1",
            "settings",
            "roles"
          ]
        }
      },
      "status": "Bad Request",
      "code": 400,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "ZGS"
        },
        {
          "key": "Date",
          "value": "Tue, 15 Jun 2021 10:23:46 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json;charset=utf-8"
        },
        {
          "key": "Content-Length",
          "value": "179"
        },
        {
          "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-06-15T11:15:35+00:00"
        },
        {
          "key": "clientVersion",
          "value": "4116249"
        },
        {
          "key": "clientsubVersion",
          "value": "0a372498832d823bc03c7945101b2b5e"
        },
        {
          "key": "Content-Disposition",
          "value": "attachment; filename=response.json"
        },
        {
          "key": "Content-Language",
          "value": "en-US"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"roles\": [\n        {\n            \"code\": \"INVALID_DATA\",\n            \"details\": {\n                \"api_name\": \"id\",\n                \"json_path\": \"$.roles[0].forecast_manager.id\"\n            },\n            \"message\": \"Forecast Manager not present in this Role\",\n            \"status\": \"error\"\n        }\n    ]\n}"
    },
    {
      "name": "INVALID_REQUEST_METHOD",
      "originalRequest": {
        "method": "PATCH",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"roles\": [\n        {\n            \"id\": \"738964000000015969\",\n            \"name\": \"Sales department Head\",\n            \"reporting_to\": \"738964000000015966\",\n            \"forecast_manager\": \"738964000000291009\",\n            \"description\": \"Manage the sales department\",\n            \"share_with_peers\": true\n        }\n    ]\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{api-domain}}/crm/v2.1/settings/roles",
          "host": [
            "{{api-domain}}"
          ],
          "path": [
            "crm",
            "v2.1",
            "settings",
            "roles"
          ]
        }
      },
      "status": "Bad Request",
      "code": 400,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "ZGS"
        },
        {
          "key": "Date",
          "value": "Tue, 15 Jun 2021 10:26:24 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": "INVALID_DATA (Role ID)",
      "originalRequest": {
        "method": "PUT",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"roles\": [\n        {\n            \"id\": \"73896400000001596\",\n            \"name\": \"Sales department Head\",\n            \"reporting_to\": \"738964000000015966\",\n            \"forecast_manager\": \"738964000000291009\",\n            \"description\": \"Manage the sales department\",\n            \"share_with_peers\": true\n        }\n    ]\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{api-domain}}/crm/v2.1/settings/roles",
          "host": [
            "{{api-domain}}"
          ],
          "path": [
            "crm",
            "v2.1",
            "settings",
            "roles"
          ]
        }
      },
      "status": "Bad Request",
      "code": 400,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "ZGS"
        },
        {
          "key": "Date",
          "value": "Tue, 15 Jun 2021 10:26:48 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json;charset=utf-8"
        },
        {
          "key": "Content-Length",
          "value": "152"
        },
        {
          "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-06-15T11:15:35+00:00"
        },
        {
          "key": "clientVersion",
          "value": "4116249"
        },
        {
          "key": "clientsubVersion",
          "value": "0a372498832d823bc03c7945101b2b5e"
        },
        {
          "key": "Content-Disposition",
          "value": "attachment; filename=response.json"
        },
        {
          "key": "Content-Language",
          "value": "en-US"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"roles\": [\n        {\n            \"code\": \"INVALID_DATA\",\n            \"details\": {\n                \"api_name\": \"id\",\n                \"json_path\": \"$.roles[0].id\"\n            },\n            \"message\": \"the given role id seems invalid\",\n            \"status\": \"error\"\n        }\n    ]\n}"
    },
    {
      "name": "INVALID_DATA (Reporting To ID)",
      "originalRequest": {
        "method": "PUT",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"roles\": [\n        {\n            \"id\": \"738964000000015969\",\n            \"name\": \"Sales department Head\",\n            \"reporting_to\": \"73896400000001596\",\n            \"description\": \"Manage the sales department\",\n            \"share_with_peers\": true\n        }\n    ]\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{api-domain}}/crm/v2.1/settings/roles",
          "host": [
            "{{api-domain}}"
          ],
          "path": [
            "crm",
            "v2.1",
            "settings",
            "roles"
          ]
        }
      },
      "status": "Bad Request",
      "code": 400,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "ZGS"
        },
        {
          "key": "Date",
          "value": "Tue, 15 Jun 2021 10:30:30 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json;charset=utf-8"
        },
        {
          "key": "Content-Length",
          "value": "165"
        },
        {
          "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-06-15T11:15:35+00:00"
        },
        {
          "key": "clientVersion",
          "value": "4116249"
        },
        {
          "key": "clientsubVersion",
          "value": "0a372498832d823bc03c7945101b2b5e"
        },
        {
          "key": "Content-Disposition",
          "value": "attachment; filename=response.json"
        },
        {
          "key": "Content-Language",
          "value": "en-US"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"roles\": [\n        {\n            \"code\": \"INVALID_DATA\",\n            \"details\": {\n                \"api_name\": \"id\",\n                \"json_path\": \"$.roles[0].reporting_to.id\"\n            },\n            \"message\": \"the given role id seems invalid\",\n            \"status\": \"error\"\n        }\n    ]\n}"
    },
    {
      "name": "DUPLICATE_DATA",
      "originalRequest": {
        "method": "PUT",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"roles\": [\n        {\n            \"id\": \"738964000002277002\",\n            \"name\": \"Manager\",\n            \"reporting_to\": \"738964000000015966\",\n            \"description\": \"Manage the sales department\",\n            \"share_with_peers\": true\n        }\n    ]\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{api-domain}}/crm/v2.1/settings/roles",
          "host": [
            "{{api-domain}}"
          ],
          "path": [
            "crm",
            "v2.1",
            "settings",
            "roles"
          ]
        }
      },
      "status": "Bad Request",
      "code": 400,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "ZGS"
        },
        {
          "key": "Date",
          "value": "Tue, 15 Jun 2021 10:32:05 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json;charset=utf-8"
        },
        {
          "key": "Content-Length",
          "value": "188"
        },
        {
          "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-06-15T11:15:35+00:00"
        },
        {
          "key": "clientVersion",
          "value": "4116249"
        },
        {
          "key": "clientsubVersion",
          "value": "0a372498832d823bc03c7945101b2b5e"
        },
        {
          "key": "Content-Disposition",
          "value": "attachment; filename=response.json"
        },
        {
          "key": "Content-Language",
          "value": "en-US"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"roles\": [\n        {\n            \"code\": \"DUPLICATE_DATA\",\n            \"details\": {\n                \"api_name\": \"name\",\n                \"json_path\": \"$.roles[0].name\"\n            },\n            \"message\": \"Failed to add role since role with same name is already exist\",\n            \"status\": \"error\"\n        }\n    ]\n}"
    },
    {
      "name": "MANDATORY_NOT_FOUND",
      "originalRequest": {
        "method": "PUT",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"roles\": [\n        {\n            \"name\": \"Manager\",\n            \"reporting_to\": \"738964000000015966\",\n            \"description\": \"Manage the sales department\",\n            \"share_with_peers\": true\n        }\n    ]\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{api-domain}}/crm/v2.1/settings/roles",
          "host": [
            "{{api-domain}}"
          ],
          "path": [
            "crm",
            "v2.1",
            "settings",
            "roles"
          ]
        }
      },
      "status": "Bad Request",
      "code": 400,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "ZGS"
        },
        {
          "key": "Date",
          "value": "Tue, 15 Jun 2021 10:32:25 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json;charset=utf-8"
        },
        {
          "key": "Content-Length",
          "value": "152"
        },
        {
          "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-06-15T11:15:35+00:00"
        },
        {
          "key": "clientVersion",
          "value": "4116249"
        },
        {
          "key": "clientsubVersion",
          "value": "0a372498832d823bc03c7945101b2b5e"
        },
        {
          "key": "Content-Disposition",
          "value": "attachment; filename=response.json"
        },
        {
          "key": "Content-Language",
          "value": "en-US"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"roles\": [\n        {\n            \"code\": \"MANDATORY_NOT_FOUND\",\n            \"details\": {\n                \"api_name\": \"id\",\n                \"json_path\": \"$.roles[0].id\"\n            },\n            \"message\": \"required field not found\",\n            \"status\": \"error\"\n        }\n    ]\n}"
    }
  ]
}