Multi-User Support
The NodeJS SDK supports both single-user and multi-user app.
Multi-user App
Multi-users functionality is achieved using the switchUser() method.
//If proxy needs to be configured for the User
(await new InitializeBuilder())
.user(user)
.environment(environment)
.token(token)
.SDKConfig(sdkConfig)
.switchUser();
Use the below code to remove a user's configuration from the SDK.
await Initializer.removeUserConfiguration(user, environment)
Sample Multi-user code
const InitializeBuilder = require( "@zohocrm/nodejs-sdk-2.1/routes/initialize_builder").InitializeBuilder;
const OAuthBuilder = require( "@zohocrm/nodejs-sdk-2.1/models/authenticator/oauth_builder").OAuthBuilder;
const UserSignature = require( "@zohocrm/nodejs-sdk-2.1/routes/user_signature").UserSignature;
const {Levels} = require( "@zohocrm/nodejs-sdk-2.1/routes/logger/logger");
const LogBuilder = require( "@zohocrm/nodejs-sdk-2.1/routes/logger/log_builder").LogBuilder;
const USDataCenter = require( "@zohocrm/nodejs-sdk-2.1/routes/dc/us_data_center").USDataCenter;
const EUDataCenter = require( "@zohocrm/nodejs-sdk-2.1/routes/dc/eu_data_center").EUDataCenter;
const DBBuilder = require( "@zohocrm/nodejs-sdk-2.1/models/authenticator/store/db_builder").DBBuilder;
const FileStore = require( "@zohocrm/nodejs-sdk-2.1/models/authenticator/store/file_store").FileStore;
const {RecordOperations} = require("@zohocrm/nodejs-sdk-2.1/core/com/zoho/crm/api/record/record_operations");
const ParameterMap = require("@zohocrm/nodejs-sdk-2.1/routes/parameter_map").ParameterMap;
const HeaderMap = require("@zohocrm/nodejs-sdk-2.1/routes/header_map").HeaderMap;
const ResponseWrapper = require("@zohocrm/nodejs-sdk-2.1/core/com/zoho/crm/api/record/response_wrapper").ResponseWrapper;
const ProxyBuilder = require( "@zohocrm/nodejs-sdk-2.1/routes/proxy_builder").ProxyBuilder;
const SDKConfigBuilder = require("@zohocrm/nodejs-sdk-2.1/routes/sdk_config_builder").MasterModel;
const GetRecordsParam = require("@zohocrm/nodejs-sdk-2.1/core/com/zoho/crm/api/record/record_operations").GetRecordsParam;
const GetRecordsHeader = require("@zohocrm/nodejs-sdk-2.1/core/com/zoho/crm/api/record/record_operations").GetRecordsHeader;
class Record {
static async call() {
let logger = new LogBuilder()
.level(Levels.INFO)
.filePath("/Users/username/final-logs.txt")
.build();
let user1 = new UserSignature("abc@zoho.com");
let environment1 = USDataCenter.PRODUCTION();
let token1 = new OAuthBuilder()
.clientId("clientId")
.clientSecret("clientSecret")
// .grantToken("grantToken")
.refreshToken("refreshToken")
.redirectURL("redirectURL")
.build();
let tokenstore = new DBBuilder()
.host("hostName")
.databaseName("databaseName")
.userName("userName")
.portNumber("portNumber")
.tableName("tableName")
.password("password")
.build();
let tokenstore = new FileStore("/Users/username/nodejs_sdk_tokens.txt");
let sdkConfig = new SDKConfigBuilder()
.pickListValidation(false)
.autoRefreshFields(true)
.build();
let resourcePath = "/Users/username";
(await new InitializeBuilder())
.user(user1)
.environment(environment1)
.token(token1)
.store(tokenstore)
.SDKConfig(sdkConfig)
.resourcePath(resourcePath)
.logger(logger)
.initialize();
await Record.getRecords("Leads");
await Initializer.removeUserConfiguration(user1, environment1);
let user2 = new UserSignature("abc2@zoho.eu");
let environment2 = EUDataCenter.SANDBOX();
let token2 = new OAuthBuilder()
.clientId("clientId")
.clientSecret("clientSecret")
.grantToken("GRANT Token")
.refreshToken("REFRESH Token")
.redirectURL("redirectURL")
.build();
let requestProxy = new ProxyBuilder()
.host("proxyHost")
.port("proxyPort")
.user("proxyUser")
.password("password")
.build();
let sdkConfig2 = new SDKConfigBuilder()
.pickListValidation(true)
.autoRefreshFields(true)
.build();
(await new InitializeBuilder())
.user(user2)
.environment(environment2)
.token(token2)
.SDKConfig(sdkConfig2)
.requestProxy(requestProxy)
.switchUser();
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.getStatusCode());
if([204, 304].includes(response.getStatusCode())){
console.log(response.getStatusCode() == 204? "No Content" : "Not Modified");
return;
}
//Get the object from response
let responseObject = response.getObject();
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 are 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 InitializeBuilder = require( "@zohocrm/nodejs-sdk-2.1/routes/initialize_builder").InitializeBuilder;
const OAuthBuilder = require( "@zohocrm/nodejs-sdk-2.1/models/authenticator/oauth_builder").OAuthBuilder;
const UserSignature = require( "@zohocrm/nodejs-sdk-2.1/routes/user_signature").UserSignature;
const {Levels} = require( "@zohocrm/nodejs-sdk-2.1/routes/logger/logger");
const LogBuilder = require( "@zohocrm/nodejs-sdk-2.1/routes/logger/log_builder").LogBuilder;
const USDataCenter = require( "@zohocrm/nodejs-sdk-2.1/routes/dc/us_data_center").USDataCenter;
const EUDataCenter = require( "@zohocrm/nodejs-sdk-2.1/routes/dc/eu_data_center").EUDataCenter;
const DBBuilder = require( "@zohocrm/nodejs-sdk-2.1/models/authenticator/store/db_builder").DBBuilder;
const FileStore = require( "@zohocrm/nodejs-sdk-2.1/models/authenticator/store/file_store").FileStore;
const {RecordOperations} = require("@zohocrm/nodejs-sdk-2.1/core/com/zoho/crm/api/record/record_operations");
const ParameterMap = require("@zohocrm/nodejs-sdk-2.1/routes/parameter_map").ParameterMap;
const HeaderMap = require("@zohocrm/nodejs-sdk-2.1/routes/header_map").HeaderMap;
const ResponseWrapper = require("@zohocrm/nodejs-sdk-2.1/core/com/zoho/crm/api/record/response_wrapper").ResponseWrapper;
const ProxyBuilder = require( "@zohocrm/nodejs-sdk-2.1/routes/proxy_builder").ProxyBuilder;
const SDKConfigBuilder = require("@zohocrm/nodejs-sdk-2.1/routes/sdk_config_builder").MasterModel;
class Record {
static async getRecords() {
/*
* Create an instance of Logger Class that requires the following
* level -> Level of the log messages to be logged. Can be configured by typing Levels "." and choose any level from the list displayed.
* filePath -> Absolute file path, where messages need to be logged.
*/
let logger = new LogBuilder()
.level(Levels.INFO)
.filePath("/Users/user_name/nodejs_sdk_log.log")
.build();
/*
* 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
* clientId -> OAuth client id.
* clientSecret -> OAuth client secret.
* grantToken -> GRANT token.
* redirectURL -> OAuth redirect URL. Default value is null
*/
let token1 = new OAuthBuilder()
.clientId("clientId")
.clientSecret("clientSecret")
.grantToken("GRANT Token")
.redirectURL("redirectURL")
.build();
/*
* Create an instance of TokenStore.
* host -> DataBase host name. Default "localhost"
* databaseName -> DataBase name. Default "zohooauth"
* userName -> DataBase user name. Default "root"
* password -> DataBase password. Default ""
* portNumber -> DataBase port number. Default "3306"
* tableName -> DataBase table name. Default value "oauthtoken"
*/
let tokenstore = new DBStore().build();
let tokenstore = new DBBuilder()
.host("hostName")
.databaseName("databaseName")
.userName("userName")
.portNumber("portNumber")
.tableName("tableName")
.password("password")
.build();
/*
* 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()
.pickListValidation(false)
.autoRefreshFields(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";
/*
* Set the following in InitializeBuilder
* user -> UserSignature instance
* environment -> Environment instance
* token -> Token instance
* store -> TokenStore instance
* SDKConfig -> sdkConfig instance
* resourcePath -> resourcePath
* logger -> Logger instance
*/
(await new InitializeBuilder())
.user(user1)
.environment(environment1)
.token(token1)
.store(tokenstore)
.SDKConfig(sdkConfig)
.resourcePath(resourcePath)
.logger(logger)
.initialize();
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.getStatusCode());
if([204, 304].includes(response.getStatusCode())){
console.log(response.getStatusCode() == 204? "No Content" : "Not Modified");
return;
}
//Get the object from response
let responseObject = response.getObject();
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
