Node JS SDK Samples - Actions & Attachment Operations
Actions
Convert a Lead
//CONVERT A LEAD
var crmclient = require('zcrmsdk');
crmclient.initialize();
var input = {};
input.id = '1000000145990'; // id: record-id of the Lead to be converted
// The Lead conversion details are constructed as a JSON object of a JSON array, which corresponds to the 'data' key
// of the input.
// Refer the 'Note' section of the API help document
// The JSON array is passed in the 'body' key of the request 'input'
input.body = JSON.parse("{\"data\": [{\"overwrite\": true,\"notify_lead_owner\": true,\"notify_new_entity_owner\": true, \"Accounts\": \"1041860000001828031\", \"Contacts\": \"1041860000001475008\", \"assign_to\": \"1041860000000067001\", \"Deals\": { \"Deal_Name\": \"Robert\", \"Closing_Date\": \"2019-03-30\", \"Stage\": \"Closed Won\", \"Amount\": 56.6 } } ] }" );
crmclient.API.ACTIONS.convert(input).then(function(response) {
// Response of the API call is returned in the 'body'
// The conversion details available as JSON array of the 'data' key of the JSON response
response = JSON.parse(response.body);
response = response.data;
console.log(response);
});
Files & Attachments
Upload an Attachment
//UPLOAD A FILE TO A RECORD
var crmclient = require('zcrmsdk');
crmclient.initialize();
var input = {};
// Filestream to read the file to be uploaded
var fs = require('fs');
// Specify the file-path of the file to be uploaded
var readStream = fs.createReadStream('<file-path>');
input.x_file_content = readStream;
input.id='3519112000000398007'; // id: record-id to which the file is to be associated
input.module = 'Leads';
crmclient.API.ATTACHMENTS.uploadFile(input).then(function(response) {
// Response of the API call is returned in the 'body'
// The file upload status is 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];
console.log(response);
// The attachment id of the uploaded file can be obtained from response.details.id
console.log(response.details.id);
});
Download an Attachment
//DOWNLOAD A FILE FROM A RECORD
var crmclient = require('zcrmsdk');
crmclient.initialize();
var input = {};
input.id='3519112000000398007'; // id: record-id
input.module = 'Leads';
input.relatedId = '3519112000000439001'; // relatedId: attchment id
input.download_file = true;
crmclient.API.ATTACHMENTS.downloadFile(input).then(function(response) {
// The content of the file is obtained from the 'body' of the response
// The file name is obtained from the 'filename' of the response
// Obtaining the file content
var content = response.body;
// Obtaining the file name
var filename = response.filename;
// Creating a file stream to write the file data
var fs = require('fs');
// Writing the file content with the filename in the local system
// If the file is to be written with a different file name, specify the same in the filename
fs.writeFile(filename, content, 'binary', function(error) {
if(error) { console.log(error); }
});
});
Delete an Attachment
//DELETE A FILE FROM A RECORD
var crmclient = require('zcrmsdk');
crmclient.initialize();
var input = {};
input.id='3519112000000398007'; // id: record-id
input.module = 'Leads';
input.relatedId = '3519112000000439001'; // relatedId: attchment id
crmclient.API.ATTACHMENTS.deleteFile(input).then(function(response) {
// Response of the API call is returned in the 'body'
// The deletion status of the attachment is 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];
console.log(response);
});
Images
Upload a Photo
//UPLOAD A PHOTO TO A LEAD OR A CONTACT
var crmclient = require('zcrmsdk');
crmclient.initialize();
var input = {};
// Filestream to read the photo file to be uploaded
var fs = require('fs');
var readStream = fs.createReadStream('<file-name with path>');
input.x_file_content = readStream;
input.id="3519112000000398007"; // id: record-id to which the photo is to be uploaded
input.module = "Leads";
crmclient.API.ATTACHMENTS.uploadPhoto(input).then(function(response) {
// Response status of the photo upload is returned in the 'body'
response = response.body;
console.log(response);
readStream.close();
});
Download a Photo
//DOWNLOAD A PHOTO
var crmclient = require('zcrmsdk');
crmclient.initialize();
var input = {};
input.id='3519112000000398007';
input.module = 'Leads';
input.download_file = true;
crmclient.API.ATTACHMENTS.downloadPhoto(input).then(function(response) {
// The content of the file is obtained from the 'body' of the response
// The file name is obtained from the 'filename' of the response
// Obtaining the file content
var content = response.body;
// Obtaining the file name
var filename = response.filename;
// Creating a file stream to write the file data
var fs = require('fs');
// Writing the file content with the filename in the local system
// If the file is to be written with a different file name, specify the same in the filename
fs.writeFile(filename, content, 'binary', function(error) {
if(error) { console.log(error); }
});
});
Delete a Photo
//DELETE A PHOTO
var crmclient = require('zcrmsdk');
crmclient.initialize();
var params = {};
params.id='3519112000000398007'; // id: record-id of the record
// whose photo is to be deleted
params.module = 'Leads';
crmclient.API.ATTACHMENTS.deletePhoto(params).then(function(response) {
// Status of the deletion request is returned in the 'body' of the response
response = JSON.parse(response.body);
console.log(response);
});