Multi-User Support
The TypeScript SDK supports both single-user and multi-user app.
Multi-user App
Multi-users functionality is achieved using the Initializer's static switchUser() method.
(await new ZOHOCRMSDK.InitializeBuilder())
.user(user)
.environment(environment)
.token(token)
.SDKConfig(sdkConfig)
.switchUser();
Use the below code to remove a user's configuration from the SDK.
await ZOHOCRMSDK.Initializer.removeUserConfiguration(user, environment)
Sample Multi-user code
import * as ZOHOCRMSDK from "@zohocrm/typescript-sdk-2.1";
class SampleRecord {
public static async call() {
/*
* Create an instance of Logger Class that takes two parameters
* 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: ZOHOCRMSDK.Logger = new ZOHOCRMSDK.LogBuilder()
.level(ZOHOCRMSDK.Levels.INFO)
.filePath("/Users/user_name/Documents/ts_sdk_log.log")
.build();
/*
* Create an UserSignature instance that takes user Email as parameter
*/
let user: ZOHOCRMSDK.UserSignature = new ZOHOCRMSDK.UserSignature("abc@zoho.com");
/*
* Configure the environment
* which is of the pattern Domain.Environment
* Available Domains: USDataCenter, EUDataCenter, INDataCenter, CNDataCenter, AUDataCenter, JPDataCenter
* Available Environments: PRODUCTION(), DEVELOPER(), SANDBOX()
*/
let environment: ZOHOCRMSDK.Environment = ZOHOCRMSDK.USDataCenter.PRODUCTION();
/*
* Create a Token instance
* clientId -> OAuth client id.
* clientSecret -> OAuth client secret.
* grantToken -> OAuth Grant Token.
* refreshToken -> OAuth Refresh Token token.
* redirectURL -> OAuth Redirect URL.
*/
let token: ZOHOCRMSDK.OAuthToken = new ZOHOCRMSDK.OAuthBuilder()
.clientId("clientId")
.clientSecret("clientSecret")
.refreshToken("refreshToken")
.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 "oauthtoken"
*/
let tokenstore: ZOHOCRMSDK.DBStore = new ZOHOCRMSDK.OAuthBuilder()
// .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 store: ZOHOCRMSDK.FileStore = new ZOHOCRMSDK.FileStore("/Users/username/ts_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.ts)
*
* 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: ZOHOCRMSDK.SDKConfig = new ZOHOCRMSDK.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: string = "/Users/user_name/Documents/ts-app";
/*
* Call the static initialize method of Initializer class that takes the following arguments
* user -> UserSignature instance
* environment -> Environment instance
* token -> Token instance
* store -> TokenStore instance
* SDKConfig -> SDKConfig instance
* resourcePath -> resourcePath
* logger -> Logger instance
*/
try {
(await new ZOHOCRMSDK.InitializeBuilder())
.user(user1)
.environment(environment1)
.token(token1)
.store(store)
.SDKConfig(sdkConfig)
.resourcePath(resourcePath)
.logger(logger)
.initialize();
} catch (error) {
console.log(error);
}
await SampleRecord.getRecords("Leads");
await ZOHOCRMSDK.Initializer.removeUserConfiguration(user1, environment1);
let user2: ZOHOCRMSDK.UserSignature = new ZOHOCRMSDK.UserSignature("abc2@zoho.eu");
let environment2: ZOHOCRMSDK.Environment = ZOHOCRMSDK.EUDataCenter.SANDBOX();
let token2: ZOHOCRMSDK.OAuthToken= = new ZOHOCRMSDK.OAuthBuilder()
.clientId("clientId")
.clientSecret("clientSecret")
.refreshToken("refreshToken")
.redirectURL("redirectURL")
.build();
let sdkConfig2: ZOHOCRMSDK.SDKConfig = new ZOHOCRMSDK.SDKConfigBuilder().pickListValidation(true).autoRefreshFields(true).build();
(await new ZOHOCRMSDK.InitializeBuilder())
.user(user2)
.environment(environment2)
.token(token2)
.SDKConfig(sdkConfig2)
.switchUser();
await SampleRecord.getRecords("Leads");
}
static async getRecords(moduleAPIName: string) {
try {
let moduleAPIName = "Leads";
//Get instance of RecordOperations Class
let recordOperations: ZOHOCRMSDK.Records.RecordOperations = new ZOHOCRMSDK.Records.RecordOperations();
let paramInstance: ZOHOCRMSDK.ParameterMap = new ZOHOCRMSDK.ParameterMap();
await paramInstance.add(ZOHOCRMSDK.Records.GetRecordsParam.APPROVED, "both");
let headerInstance: ZOHOCRMSDK.HeaderMap = new ZOHOCRMSDK.HeaderMap();
await headerInstance.add(ZOHOCRMSDK.Records.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: ZOHOCRMSDK.APIResponse<ZOHOCRMSDK.Records.ResponseHandler.MasterModel> = 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: ZOHOCRMSDK.Records.ResponseHandler.MasterModel = response.getObject();
if(responseObject != null){
//Check if expected ResponseWrapper instance is received
if(responseObject instanceof ZOHOCRMSDK.Records.ResponseWrapper){
//Get the array of obtained Record instances
let records: ZOHOCRMSDK.Records.Record[] = responseObject.getData();
for (let record of records) {
//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: ZOHOCRMSDK.Tags.Tag[] = 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());
});
}
//To get particular field value
console.log("Record Field Value: " + record.getKeyValue("Last_Name"));// FieldApiName
console.log("Record KeyValues: " );
let keyValues: Map<string,any> = record.getKeyValues();
let keyArray: string[] = Array.from(keyValues.keys());
for (let keyName of keyArray) {
let value: any = keyValues.get(keyName);
console.log(keyName + " : " + value);
}
}
}
}
}
} catch (error) {
console.log(error);
}
}
}
SampleRecord.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 are given in the variables user2, token2, environment2
Then, the switchUser() function is used to switch between the User1 and User2 as required.
Based on the latest switched user, the SampleRecord.getRecords(moduleAPIName) will fetch records.
SDK Sample Code
import * as ZOHOCRMSDK from "@zohocrm/typescript-sdk-2.1";
class SampleRecord {
public static async getRecords(){
let user: ZOHOCRMSDK.UserSignature = new ZOHOCRMSDK.UserSignature("abc@zoho.com");
let myLogger: ZOHOCRMSDK.Logger = new ZOHOCRMSDK.LogBuilder()
.level(ZOHOCRMSDK.Levels.INFO)
.filePath("/Users/user_name/Documents/ts_sdk_log.log")
.build();
let dc: ZOHOCRMSDK.Environment = ZOHOCRMSDK.USDataCenter.PRODUCTION();
let sdkConfig: ZOHOCRMSDK.SDKConfig = new ZOHOCRMSDK.SDKConfigBuilder().autoRefreshFields(false).pickListValidation(true).build();
let store: ZOHOCRMSDK.FileStore = new ZOHOCRMSDK.FileStore("/Users/username/Documents/ts_sdk_tokens.txt");
let token: ZOHOCRMSDK.OAuthToken= new ZOHOCRMSDK.OAuthBuilder()
.clientId("clientId")
.clientSecret("clientSecret")
.refreshToken("refreshToken")
.redirectURL("redirectURL")
.build();
let path: string = "/Users/user_name/Documents/ts-app";
try {
(await new ZOHOCRMSDK.InitializeBuilder())
.user(user)
.environment(dc)
.token(token)
.store(store)
.SDKConfig(sdkConfig)
.resourcePath(path)
.logger(myLogger)
.initialize();
} catch (error) {
console.log(error);
}
try {
let moduleAPIName = "Leads";
//Get instance of RecordOperations Class
let recordOperations: ZOHOCRMSDK.Records.RecordOperations = new ZOHOCRMSDK.Records.RecordOperations();
let paramInstance: ZOHOCRMSDK.ParameterMap = new ZOHOCRMSDK.ParameterMap();
await paramInstance.add(ZOHOCRMSDK.Records.GetRecordsParam.APPROVED, "both");
let headerInstance: ZOHOCRMSDK.HeaderMap = new ZOHOCRMSDK.HeaderMap();
await headerInstance.add(ZOHOCRMSDK.Records.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: ZOHOCRMSDK.APIResponse<ZOHOCRMSDK.Records.ResponseHandler.MasterModel> = 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: ZOHOCRMSDK.Records.ResponseHandler.MasterModel = response.getObject();
if(responseObject != null) {
//Check if expected ResponseWrapper instance is received
if(responseObject instanceof ZOHOCRMSDK.Records.ResponseWrapper){
//Get the array of obtained Record instances
let records: ZOHOCRMSDK.Records.Record[] = responseObject.getData();
for (let record of records) {
//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: ZOHOCRMSDK.Tags.Tag[] = 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());
});
}
//To get particular field value
console.log("Record Field Value: " + record.getKeyValue("Last_Name"));// FieldApiName
console.log("Record KeyValues: " );
let keyValues: Map<string,any> = record.getKeyValues();
let keyArray: string[] = Array.from(keyValues.keys());
for (let keyName of keyArray) {
let value: any = keyValues.get(keyName);
console.log(keyName + " : " + value);
}
}
}
}
}
} catch (error) {
console.log(error);
}
}
}
SampleRecord.getRecords();
Record Response
