Node JS SDK Samples - Module Operations
//GET MULTIPLE RECORDS
var crmclient = require('zcrmsdk');
crmclient.initialize();
var input = {};
input.module = 'Leads';
var params = {};
// See the list of possible parameters in the 'Parameters' section of the API help page
params.page = 0;
params.per_page = 5;
var headers = {};
// See the list of possible headers in the 'Request Headers' section of the API help page
headers['If-Modified-Since'] = '2019-01-07T14:56:33+05:30';
input.params = params;
input.headers = headers;
crmclient.API.MODULES.get(input).then(function(response) {
// Response of the API call is returned in the 'body'
// Records data value available as JSON Array of the 'data' key of the JSON response
// Each JSON object of the array corresponds to a record
response_data = JSON.parse(response.body).data;
// Iterating the JSON array
for(record in response_data) {
// Use response_data[record].<api-name of field> to obtain a specific field value
// Sample API names: Full_Name, Email
// List of all available keys of the response can be seen in the JSON object corresponding to
// a record. The entire object containig all keys can be obtained using 'response_data[record]'
console.log(response_data[record].Full_Name);
}
// Information value regarding the processed request available in the 'info' key of the JSON response
// Sample field values of the 'info' of the response: per_page, count, page, more_records
response_info = JSON.parse(response.body).info;
console.log(response_info.more_records);
});
//GET A SPECIFIC RECORD
var crmclient = require('zcrmsdk');
crmclient.initialize();
input = {};
input.id = '3519112000000398007'; // id: record-id
input.module = 'Leads';
crmclient.API.MODULES.get(input).then(function(response) {
// Response of the API call is returned in the 'body'
// The specific record details are obtained from the first JSON object of the JSON Array corresponding
// to the 'data' key of the response
response = JSON.parse(response.body);
response = response.data[0];
// For obtaining all the fields of the record details, use the value of 'response' as such
console.log(response);
// For obtaining a particular field, use response.<api-name of field>
// Sample API names: Website, Email
console.log(response.Email);
});
//INSERT RECORDS
var crmclient = require('zcrmsdk');
crmclient.initialize();
// Records to be inserted are constructed as JSON objects of a JSON array, which corresponds to the 'data' key
// of the input
// The field names and their values are sent as key-value pairs of the JSON object of a record
// The field API names can be found at Setup -> DEVELOPER SPACE -> APIs in the online CRM account view
// For example, 'Company' and 'Last_Name' are API names of fields of the Leads module
var leadJSON = JSON.parse(`{ \"data\": [ { \"Company\": \"Zylker\", \"Last_Name\": \"Patrica\" }, { \"Company\": \"Zylker\", \"Last_Name\": \"Deborah\" } ] }`);
var input = {};
input.module = 'Leads';
// The JSON array is sent in the 'body' element of the 'input' of the request
input.body = leadJSON;
crmclient.API.MODULES.post(input).then(function(response) {
// Response of the API call is returned in the 'body'
// The result of every individual record insersion is returned as a JSON object of the
// JSON array corresponding to the 'data' key of the response
response = JSON.parse(response.body);
response = response.data;
console.log(response);
// To obtain the details of the individual record insersions, iterate the JSON array
// Iterating the JSON array
for(record in response) {
var info = response[record];
console.log(info);
// To obtain the record-id of the created records, use info.details.id
console.log(info.details.id);
}
});
//UPDATE RECORDS
var crmclient = require('zcrmsdk');
crmclient.initialize();
// Records to be inserted are constructed as JSON objects of a JSON array, which corresponds to the 'data' key
// of the input. The fields whose values are to be updated are specified as key-value pairs for each record
// The field API names can be found at Setup -> DEVELOPER SPACE -> APIs in the online CRM account view
// For example, 'Company' and 'Last_Name' are API names of fields of the Leads module
// The record is identified by the 'id' of the record, which is mandatorily sent as a key in the request body
var leadJSON = JSON.parse(`{ \"data\": [ { \"id\":\"3519112000000424009\",\"Company\": \"Zylker\", \"Last_Name\": \"Patricia\" }, { \"id\":\"3519112000000424010\",\"Company\": \"Zylker\", \"Last_Name\": \"Deborah\" } ] }`);
var input = {};
input.module = 'Leads';
// The JSON array is sent in the 'body' element of the 'input' of the request
input.body = leadJSON;
crmclient.API.MODULES.put(input).then(function(response){
// The result of the every individual record update is returned as a JSON object of the
// JSON array corresponding to the 'data' key of the response
response = JSON.parse(response.body);
response = response.data;
console.log(response);
});
//UPDATE A SPECIFIC RECORD
var crmclient = require('zcrmsdk');
crmclient.initialize();
// Record to be updated is constructed as a JSON object of a JSON array, which corresponds to the 'data' key
// of the input. The fields whose values are to be updated are specified as key-value pairs for each record
// The field API names can be found at Setup -> DEVELOPER SPACE -> APIs in the online CRM account view
// For example, 'Company' and 'Last_Name' are API names of fields of the Leads module
var leadJSON = JSON.parse("{ \"data\": [ { \"Company\": \"Zylker\", \"Last_Name\": \"Patricia\" } ] }");
var input = {};
input.module = 'Leads';
input.id = '3519112000000421008'; // id: record-id of the record to be updated
// The JSON array is sent in the 'body' element of the 'input' of the request
input.body = leadJSON;
crmclient.API.MODULES.put(input).then(function(response) {
// The result of record update is returned as the first JSON object of the
// JSON array corresponding to the 'data' key of the response
response = JSON.parse(response.body);
response = response.data[0];
console.log(response);
});
//DELETE A SPECIFIC RECORD
var crmclient = require('zcrmsdk');
crmclient.initialize();
var input = {};
input.module = 'Leads';
input.id = '3519112000000421008'; // id: record-id of the record to be deleted
crmclient.API.MODULES.delete(input).then(function(response) {
// The result of record deletion is returned as the first JSON object of the
// JSON array corresponding to the 'data' key of the response
response = JSON.parse(response.body);
response = response.data[0];
console.log(response);
});
//GET DELETED RECORDS
var crmclient = require('zcrmsdk');
crmclient.initialize();
var input = {};
input.module = 'Leads';
// Response of the API call is returned in the 'body'
// Records data value available as JSON Array of the 'data' key of the JSON response
// Each JSON object of the array corresponds to a record
// All deleted records
crmclient.API.MODULES.getAllDeletedRecords(input).then(function(response) {
response = JSON.parse(response.body);
response = response.data;
console.log(response);
});
// Recycle bin records
crmclient.API.MODULES.getRecycleBinRecords(input).then(function(response) {
response = JSON.parse(response.body);
response = response.data;
console.log(response);
});
// Permanently deleted records
crmclient.API.MODULES.getPermanentlyDeletedRecords(input).then(function(response) {
response = JSON.parse(response.body);
response = response.data;
console.log(response);
});
//SEARCH RECORDS
var crmclient = require('zcrmsdk');
crmclient.initialize();
// Records can be searched either using the 'criteria' param
// or pre-defined search params like 'email', 'phone'
// For details, refer to the 'Parameters' section of the API help page
var params = {};
params.phone = '888-555-2145';
var input ={};
input.module = 'Leads';
input.params = params;
crmclient.API.MODULES.search(input).then(function(response) {
// Response of the API call is returned in the 'body'
// Records data value available as JSON Array of the 'data' key of the JSON response
// Each JSON object of the array corresponds to a record
response_data = JSON.parse(response.body).data;
// Iteration the JSON array
for(record in response_data) {
// Use response_data[record].<api-name of field> to obtain a specific field value
// Sample API names: Full_Name, Email
// List of all available keys of the response can be seen in the JSON object corresponding to
// a record. The entire object containig all keys can be obtained using 'response_data[record]'
console.log(response_data[record].Full_Name);
}
// Information value regarding the processed request available in the 'info' key of the JSON response
// Sample field values of the 'info' of the response: per_page, count, page, more_records
response_info = JSON.parse(response.body).info;
console.log(response_info.more_records);
});