Multi-User Support
The NodeJS SDK(from version 1.x.x) supports both single-user and multi-user app.
Multi-user App
Multi-users functionality is achieved using the Initializer's static switchUser method.
//If proxy needs to be configured for the User
await Initializer.switchUser(user, environment, token, sdkConfig, requestProxy)
//Without proxy
await Initializer.switchUser(user, environment, token, sdkConfig)
Use the below code to remove a user's configuration from the SDK.
await Initializer.removeUserConfiguration(user, environment)
Sample Multi-user code
const Initializer= require( "@zohocrm/nodejs-sdk-2.0/routes/initializer").Initializer;
const {OAuthToken, TokenType}= require( "@zohocrm/nodejs-sdk-2.0/models/authenticator/oauth_token");
const UserSignature = require( "@zohocrm/nodejs-sdk-2.0/routes/user_signature").UserSignature;
const {Logger, Levels}= require( "@zohocrm/nodejs-sdk-2.0/routes/logger/logger");
const USDataCenter = require( "@zohocrm/nodejs-sdk-2.0/routes/dc/us_data_center").USDataCenter;
const EUDataCenter = require( "@zohocrm/nodejs-sdk-2.0/routes/dc/eu_data_center").EUDataCenter;
const DBStore = require( "@zohocrm/nodejs-sdk-2.0/models/authenticator/store/db_store").DBStore;
const FileStore = require( "@zohocrm/nodejs-sdk-2.0/models/authenticator/store/file_store").FileStore;
const {RecordOperations, GetRecordsHeader, GetRecordsParam} = require("@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/record/record_operations");
const ParameterMap = require("@zohocrm/nodejs-sdk-2.0/routes/parameter_map").ParameterMap;
const HeaderMap = require("@zohocrm/nodejs-sdk-2.0/routes/header_map").HeaderMap;
const ResponseWrapper = require("@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/record/response_wrapper").ResponseWrapper;
const RequestProxy = require("@zohocrm/nodejs-sdk-2.0/routes/request_proxy").RequestProxy;
const SDKConfigBuilder = require("@zohocrm/nodejs-sdk-2.0/routes/sdk_config_builder").MasterModel;
class Record{
static async call(){
/*
* Create an instance of Logger Class that takes two parameters
* 1 -> Level of the log messages to be logged. Can be configured by typing Levels "." and choose any level from the list displayed.
* 2 -> Absolute file path, where messages need to be logged.
*/
let logger = Logger.getInstance(Levels.INFO, "/Users/user_name/Documents/nodejs_sdk_log.log");
/*
* Create an UserSignature instance that takes user Email as parameter
*/
let user1 = new UserSignature("abc@zoho.com");
/*
* Configure the environment
* which is of the pattern Domain.Environment
* Available Domains: USDataCenter, EUDataCenter, INDataCenter, CNDataCenter, AUDataCenter
* Available Environments: PRODUCTION(), DEVELOPER(), SANDBOX()
*/
let environment1 = USDataCenter.PRODUCTION();
/*
* Create a Token instance
* 1 -> OAuth client id.
* 2 -> OAuth client secret.
* 3 -> REFRESH/GRANT token.
* 4 -> token type.
* 5 -> OAuth redirect URL. Default value is null
*/
let token1 = new OAuthToken("clientId1", "clientSecret1", "REFRESH/ GRANT Token", TokenType.REFRESH/TokenType.GRANT, "redirectURL");
/*
* Create an instance of TokenStore.
* 1 -> DataBase host name. Default "localhost"
* 2 -> DataBase name. Default "zohooauth"
* 3 -> DataBase user name. Default "root"
* 4 -> DataBase password. Default ""
* 5 -> DataBase port number. Default "3306"
*/
let tokenstore = new DBStore();
let tokenstore = new DBStore("hostName", "dataBaseName", "userName", "password", "portNumber");
/*
* Create an instance of FileStore that takes absolute file path as parameter
*/
let tokenstore = new FileStore("/Users/username/Documents/nodejs_sdk_tokens.txt");
/*
* autoRefreshFields
* if true - all the modules' fields will be auto-refreshed in the background, every hour.
* if false - the fields will not be auto-refreshed in the background. The user can manually delete the file(s) or refresh the fields using methods from ModuleFieldsHandler(utils/util/module_fields_handler.js)
*
* pickListValidation
* A boolean field that validates user input for a pick list field and allows or disallows the addition of a new value to the list.
* True - the SDK validates the input. If the value does not exist in the pick list, the SDK throws an error.
* False - the SDK does not validate the input and makes the API request with the user’s input to the pick list
*/
let sdkConfig = new SDKConfigBuilder().setPickListValidation(false).setAutoRefreshFields(true).build();
/*
* The path containing the absolute directory path to store user specific JSON files containing module fields information.
*/
let resourcePath = "/Users/user_name/Documents/nodejs-app";
/*
* Call the static initialize method of Initializer class that takes the following arguments
* 1 -> UserSignature instance
* 2 -> Environment instance
* 3 -> Token instance
* 4 -> TokenStore instance
* 5 -> SDKConfig instance
* 6 -> resourcePath
* 7 -> Logger instance. Default value is null
* 8 -> RequestProxy instance. Default value is null
*/
await Initializer.initialize(user1, environment1, token1, tokenstore, sdkConfig, resourcePath, logger);
await Record.getRecords("Leads");
await Initializer.removeUserConfiguration(user1, environment1);
let user2 = new UserSignature("abc2@zoho.eu");
let environment2 = EUDataCenter.SANDBOX();
let token2 = new OAuthToken("clientId2", "clientSecret2", "REFRESH/ GRANT Token", TokenType.REFRESH/TokenType.GRANT, "redirectURL");
let requestProxy = new RequestProxy("proxyHost", 80, "proxyUser", "password");
let sdkConfig2 = new SDKConfigBuilder().setPickListValidation(true).setAutoRefreshFields(true).build();
await Initializer.switchUser(user2, environment2, token2, sdkConfig2, requestProxy);
await Record.getRecords("Leads");
}
static async getRecords(moduleAPIName){
try {
//Get instance of RecordOperations Class
let recordOperations = new RecordOperations();
let paramInstance = new ParameterMap();
await paramInstance.add(GetRecordsParam.APPROVED, "both");
let headerInstance = new HeaderMap();
await headerInstance.add(GetRecordsHeader.IF_MODIFIED_SINCE, new Date("2020-01-01T00:00:00+05:30"));
//Call getRecords method that takes paramInstance, headerInstance and moduleAPIName as parameters
let response = await recordOperations.getRecords(moduleAPIName, paramInstance, headerInstance);
if(response != null){
//Get the status code from response
console.log("Status Code: " + response.statusCode);
if([204, 304].includes(response.statusCode)){
console.log(response.statusCode == 204? "No Content" : "Not Modified");
return;
}
//Get the object from response
let responseObject = response.object;
if(responseObject != null){
//Check if expected ResponseWrapper instance is received
if(responseObject instanceof ResponseWrapper){
//Get the array of obtained Record instances
let records = responseObject.getData();
for (let index = 0; index <records.length; index++) {
let record = records[index];
//Get the ID of each Record
console.log("Record ID: " + record.getId());
//Get the createdBy User instance of each Record
let createdBy = record.getCreatedBy();
//Check if createdBy is not null
if(createdBy != null) {
//Get the ID of the createdBy User
console.log("Record Created By User-ID: " + createdBy.getId());
//Get the name of the createdBy User
console.log("Record Created By User-Name: " + createdBy.getName());
//Get the Email of the createdBy User
console.log("Record Created By User-Email: " + createdBy.getEmail());
}
//Get the CreatedTime of each Record
console.log("Record CreatedTime: " + record.getCreatedTime());
//Get the modifiedBy User instance of each Record
let modifiedBy = record.getModifiedBy();
//Check if modifiedBy is not null
if(modifiedBy != null){
//Get the ID of the modifiedBy User
console.log("Record Modified By User-ID: " + modifiedBy.getId());
//Get the name of the modifiedBy User
console.log("Record Modified By User-Name: " + modifiedBy.getName());
//Get the Email of the modifiedBy User
console.log("Record Modified By User-Email: " + modifiedBy.getEmail());
}
//Get the ModifiedTime of each Record
console.log("Record ModifiedTime: " + record.getModifiedTime());
//Get the list of Tag instance each Record
let tags = record.getTag();
//Check if tags is not null
if(tags != null){
tags.forEach(tag => {
//Get the Name of each Tag
console.log("Record Tag Name: " + tag.getName());
//Get the Id of each Tag
console.log("Record Tag ID: " + tag.getId());
});
}
//Get all the values
let keyValues = record.getKeyValues();
let keyArray = Array.from(keyValues.keys());
for (let keyIndex = 0; keyIndex <keyArray.length; keyIndex++) {
const keyName = keyArray[keyIndex];
let value = keyValues.get(keyName);
console.log(keyName + " : " + value);
}
}
}
}
}
} catch (error) {
console.log(error);
}
}
}
Record.call();
The program execution starts from call()
The details of user1 is given in the variables user1, token1, environment1.
Similarly, the details of another user user2 is given in the variables user2, token2, environment2
Then, the switchUser() function is used to switch between the User 1 and User 2 as required.
Based on the latest switched user, the Record.getRecords(moduleAPIName) will fetch record.
SDK Sample Code
const Initializer= require( "@zohocrm/nodejs-sdk-2.0/routes/initializer").Initializer;
const {OAuthToken, TokenType}= require( "@zohocrm/nodejs-sdk-2.0/models/authenticator/oauth_token");
const UserSignature = require( "@zohocrm/nodejs-sdk-2.0/routes/user_signature").UserSignature;
const {Logger, Levels}= require( "@zohocrm/nodejs-sdk-2.0/routes/logger/logger");
const USDataCenter = require( "@zohocrm/nodejs-sdk-2.0/routes/dc/us_data_center").USDataCenter;
const DBStore = require( "@zohocrm/nodejs-sdk-2.0/models/authenticator/store/db_store").DBStore;
const FileStore = require( "@zohocrm/nodejs-sdk-2.0/models/authenticator/store/file_store").FileStore;
const {RecordOperations, GetRecordsHeader, GetRecordsParam} = require("@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/record/record_operations");
const ParameterMap = require("@zohocrm/nodejs-sdk-2.0/routes/parameter_map").ParameterMap;
const HeaderMap = require("@zohocrm/nodejs-sdk-2.0/routes/header_map").HeaderMap;
const ResponseWrapper = require("@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/record/response_wrapper").ResponseWrapper;
const SDKConfigBuilder = require("@zohocrm/nodejs-sdk-2.0/routes/sdk_config_builder").MasterModel;
class Record{
static async getRecords(){
/*
* Create an instance of Logger Class that takes two parameters
* 1 -> Level of the log messages to be logged. Can be configured by typing Levels "." and choose any level from the list displayed.
* 2 -> Absolute file path, where messages need to be logged.
*/
let logger = Logger.getInstance(Levels.INFO, "/Users/user_name/Documents/nodejs_sdk_log.log");
/*
* Create an UserSignature instance that takes user Email as parameter
*/
let user = new UserSignature("abc@zoho.com");
/*
* Configure the environment
* which is of the pattern Domain.Environment
* Available Domains: USDataCenter, EUDataCenter, INDataCenter, CNDataCenter, AUDataCenter
* Available Environments: PRODUCTION(), DEVELOPER(), SANDBOX()
*/
let environment = USDataCenter.PRODUCTION();
/*
* Create a Token instance
* 1 -> OAuth client id.
* 2 -> OAuth client secret.
* 3 -> REFRESH/GRANT token.
* 4 -> token type.
* 4 -> OAuth redirect URL. Default value is null
*/
let token = new OAuthToken("clientId", "clientSecret", "REFRESH/ GRANT Token", TokenType.REFRESH/TokenType.GRANT);
/*
* Create an instance of TokenStore.
* 1 -> DataBase host name. Default "localhost"
* 2 -> DataBase name. Default "zohooauth"
* 3 -> DataBase user name. Default "root"
* 4 -> DataBase password. Default ""
* 5 -> DataBase port number. Default "3306"
*/
let tokenstore = new DBStore();
let tokenstore = new DBStore("hostName", "dataBaseName", "userName", "password", "portNumber");
/*
* Create an instance of FileStore that takes absolute file path as parameter
*/
let tokenstore = new FileStore("/Users/username/Documents/nodejs_sdk_tokens.txt");
/*
* autoRefreshFields
* if true - all the modules' fields will be auto-refreshed in the background, every hour.
* if false - the fields will not be auto-refreshed in the background. The user can manually delete the file(s) or refresh the fields using methods from ModuleFieldsHandler(utils/util/module_fields_handler.js)
*
* pickListValidation
* A boolean field that validates user input for a pick list field and allows or disallows the addition of a new value to the list.
* True - the SDK validates the input. If the value does not exist in the pick list, the SDK throws an error.
* False - the SDK does not validate the input and makes the API request with the user’s input to the pick list
*/
let sdkConfig = new SDKConfigBuilder().setPickListValidation(false).setAutoRefreshFields(true).build();
/*
* The path containing the absolute directory path to store user specific JSON files containing module fields information.
*/
let resourcePath = "/Users/user_name/Documents/nodejs-app";
/*
* Call the static initialize method of Initializer class that takes the following arguments
* 1 -> UserSignature instance
* 2 -> Environment instance
* 3 -> Token instance
* 4 -> TokenStore instance
* 5 -> sdkConfig instance
* 6 -> resourcePath
* 7 -> Logger instance
*/
await Initializer.initialize(user, environment, token, tokenstore, sdkConfig, resourcePath, logger);
try {
let moduleAPIName = "Leads";
//Get instance of RecordOperations Class
let recordOperations = new RecordOperations();
let paramInstance = new ParameterMap();
await paramInstance.add(GetRecordsParam.APPROVED, "both");
let headerInstance = new HeaderMap();
await headerInstance.add(GetRecordsHeader.IF_MODIFIED_SINCE, new Date("2020-01-01T00:00:00+05:30"));
//Call getRecords method that takes paramInstance, headerInstance and moduleAPIName as parameters
let response = await recordOperations.getRecords(moduleAPIName, paramInstance, headerInstance);
if(response != null){
//Get the status code from response
console.log("Status Code: " + response.statusCode);
if([204, 304].includes(response.statusCode)){
console.log(response.statusCode == 204? "No Content" : "Not Modified");
return;
}
//Get the object from response
let responseObject = response.object;
if(responseObject != null){
//Check if expected ResponseWrapper instance is received
if(responseObject instanceof ResponseWrapper){
//Get the array of obtained Record instances
let records = responseObject.getData();
for (let index = 0; index < records.length; index++) {
let record = records[index];
//Get the ID of each Record
console.log("Record ID: " + record.getId());
//Get the createdBy User instance of each Record
let createdBy = record.getCreatedBy();
//Check if createdBy is not null
if(createdBy != null) {
//Get the ID of the createdBy User
console.log("Record Created By User-ID: " + createdBy.getId());
//Get the name of the createdBy User
console.log("Record Created By User-Name: " + createdBy.getName());
//Get the Email of the createdBy User
console.log("Record Created By User-Email: " + createdBy.getEmail());
}
//Get the CreatedTime of each Record
console.log("Record CreatedTime: " + record.getCreatedTime());
//Get the modifiedBy User instance of each Record
let modifiedBy = record.getModifiedBy();
//Check if modifiedBy is not null
if(modifiedBy != null){
//Get the ID of the modifiedBy User
console.log("Record Modified By User-ID: " + modifiedBy.getId());
//Get the name of the modifiedBy User
console.log("Record Modified By User-Name: " + modifiedBy.getName());
//Get the Email of the modifiedBy User
console.log("Record Modified By User-Email: " + modifiedBy.getEmail());
}
//Get the ModifiedTime of each Record
console.log("Record ModifiedTime: " + record.getModifiedTime());
//Get the list of Tag instance each Record
let tags = record.getTag();
//Check if tags is not null
if(tags != null) {
tags.forEach(tag => {
//Get the Name of each Tag
console.log("Record Tag Name: " + tag.getName());
//Get the Id of each Tag
console.log("Record Tag ID: " + tag.getId());
});
}
//Get all the values
let keyValues = record.getKeyValues();
let keyArray = Array.from(keyValues.keys());
for (let keyIndex = 0; keyIndex <keyArray.length; keyIndex++) {
const keyName = keyArray[keyIndex];
let value = keyValues.get(keyName);
console.log(keyName + " : " + value);
}
}
}
}
}
} catch (error) {
console.log(error);
}
}
}
Record.getRecords();
Record Response
