|
/****************************************** |
|
* Zoho CRM Get API Class |
|
* Use it to pull data out of Zoho CRM |
|
* Version 1.0 |
|
* Created By: Russ Savage |
|
* FreeAdWordsScripts.com |
|
******************************************/ |
|
//For more info about the Zoho CRM API, see here: |
|
// https://www.zoho.com/crm/help/api/ |
|
function ZohoApi(authToken) { |
|
var ZOHO_AUTH_TOKEN = authToken; |
|
var ZOHO_BASE_URL = 'https://crm.zoho.com/crm/private/json/'; |
|
var METHODS = ['getMyRecords','getRecords','getRecordById','getCVRecords', |
|
'getSearchRecords','getSearchRecordsByPDC','getRelatedRecords', |
|
'getFields','getUsers','downloadFile','downloadPhoto']; |
|
var OBJECTS = ['Leads','Accounts','Contacts','Potentials', |
|
'Campaigns','Cases','Soultions','Products', |
|
'PriceBooks','Quotes','Invoices','SalesOrders', |
|
'Vendors','PurchaseOrders','Events','Tasks','Calls']; |
|
|
|
for(var i in OBJECTS) { |
|
// Creating getPotentials() functions |
|
this['get'+OBJECTS[i]] = new Function('additionalParams', |
|
'return this.get("'+OBJECTS[i]+'","getRecords",additionalParams);'); |
|
// Creating getMyPotentials() functions |
|
this['getMy'+OBJECTS[i]] = new Function('additionalParams', |
|
'return this.get("'+OBJECTS[i]+'","getMyRecords",additionalParams);'); |
|
// Creating getPotentialsById(id) functions |
|
this['get'+OBJECTS[i]+'ById'] = new Function('id', |
|
'return this.get("'+OBJECTS[i]+'","getRecordById",{ id : id });'); |
|
// Creating searchPotentials(searchCondition) functions |
|
this['search'+OBJECTS[i]] = new Function('criteria', |
|
'return this.get("'+OBJECTS[i]+'","getSearchRecords",criteria);'); |
|
// Creating getPotentialsFields() functions |
|
this['get'+OBJECTS[i]+'Fields'] = new Function('return this.get("'+OBJECTS[i]+'","getFields",{});'); |
|
} |
|
|
|
// You can use any Zoho OBJECT and METHOD and |
|
// put any additional parameters as a map {param : val, param2: val2} |
|
this.get = function(zohoObj,zohoMethod,additionalParams) { |
|
validateParams(zohoObj,zohoMethod); |
|
additionalParams = addColumnsIfNeeded(this,zohoObj,zohoMethod,additionalParams); |
|
var url = buildUrl(zohoObj,zohoMethod,additionalParams); |
|
Logger.log(url); |
|
var resp = UrlFetchApp.fetch(url).getContentText(); |
|
try { |
|
var jsonObj = JSON.parse(resp); |
|
if(jsonObj['response'] && jsonObj['response']['nodata']) { |
|
Logger.log('Code: '+jsonObj['response']['nodata']['code']+ |
|
' Message: '+ERRORS[jsonObj['response']['nodata']['code']]); |
|
return []; |
|
} |
|
if(jsonObj['response'] && jsonObj['response']['error']) { |
|
throw 'Code: '+jsonObj['response']['error']['code']+ |
|
' Message: '+jsonObj['response']['error']['message']; |
|
} |
|
|
|
if(jsonObj['response'] && jsonObj['response']['result']) { |
|
return parseResponseObject(zohoObj,jsonObj); |
|
} |
|
if(jsonObj[zohoObj] && jsonObj[zohoObj]['section']) { |
|
return parseFieldsObject(zohoObj,jsonObj); |
|
} |
|
return jsonObj; |
|
}catch(e){ |
|
throw 'There was an issue parsing the response. '+e; |
|
} |
|
}; |
|
|
|
function parseResponseObject(zohoObj,jsonObj) { |
|
if(jsonObj['response'] && jsonObj['response']['result']) { |
|
var rows = jsonObj['response']['result'][zohoObj]['row']; |
|
if(typeof rows[0] === 'undefined') { |
|
return [mapValToContent(rows)]; |
|
} else { |
|
var retVal = []; |
|
for(var i in rows) { |
|
retVal.push(mapValToContent(rows[i])); |
|
} |
|
return retVal; |
|
} |
|
} |
|
return []; |
|
} |
|
|
|
function parseFieldsObject(zohoObj,jsonObj) { |
|
if(jsonObj[zohoObj] && jsonObj[zohoObj]['section']) { |
|
var fields = []; |
|
for(var i in jsonObj[zohoObj]['section']) { |
|
var elem = jsonObj[zohoObj]['section'][i]; |
|
if(elem['FL'] && elem['FL'][0]) { |
|
for(var x in elem['FL']) { |
|
var field = elem['FL'][x]; |
|
if(field['dv']) { |
|
fields.push(field['dv']); |
|
} |
|
} |
|
} else if(elem['FL'] && elem['FL']['dv']) { |
|
fields.push(elem['FL']['dv']); |
|
} |
|
} |
|
return fields; |
|
} |
|
return []; |
|
} |
|
|
|
function validateParams(zohoObj,zohoMethod) { |
|
if(!zohoObj || OBJECTS.indexOf(zohoObj) == -1) { |
|
throw 'Get must be called with a proper ZOHO object. Object given: "'+ |
|
zohoObj+'" Available Objects:'+OBJECTS.join(','); |
|
} |
|
if(!zohoMethod || METHODS.indexOf(zohoMethod) == -1) { |
|
throw 'Get must be called with a proper ZOHO method. Method given: "'+ |
|
zohoObj+'" Available Methods:'+METHODS.join(','); |
|
} |
|
} |
|
|
|
function addColumnsIfNeeded(self,zohoObj,zohoMethod,additionalParams) { |
|
var searchConditionRequired = ['getSearchRecords','getSearchRecordsByPDC']; |
|
if(searchConditionRequired.indexOf(zohoMethod) >= 0) { |
|
if(!additionalParams['selectColumns']) { |
|
additionalParams['selectColumns'] = zohoObj+'('+self['get'+zohoObj+'Fields']().join(',')+')'; |
|
} |
|
} |
|
return additionalParams; |
|
} |
|
|
|
function buildUrl(zohoObj,zohoMethod,additionalParams) { |
|
var url = ZOHO_BASE_URL+zohoObj+'/'+zohoMethod+ |
|
'?authtoken='+ZOHO_AUTH_TOKEN+'&scope=crmapi&newFormat=1'; |
|
for(var key in additionalParams) { |
|
url += '&' + key + '=' + encodeURIComponent(additionalParams[key]); |
|
} |
|
return url; |
|
} |
|
|
|
function mapValToContent(obj) { |
|
var retVal = {}; |
|
if(obj.FL) { |
|
for(var i in obj.FL) { |
|
var elem = obj.FL[i]; |
|
var key = elem.val; |
|
var cleanKey = key.toLowerCase().replace(/ /g,'_'); |
|
retVal[cleanKey] = elem.content; |
|
} |
|
} |
|
return retVal; |
|
} |
|
|
|
var ERRORS = {'4000':'Please use Authtoken, instead of API ticket and APIkey.', |
|
'4500':'Internal server error while processing this request', |
|
'4501':'API Key is inactive', |
|
'4502':'This module is not supported in your edition', |
|
'4401':'Mandatory field missing', |
|
'4600':'Incorrect API parameter or API parameter value. Also check the method '+ |
|
'name and/or spelling errors in the API url.', |
|
'4820':'API call cannot be completed as you have exceeded the "rate limit".', |
|
'4831':'Missing parameters error', |
|
'4832':'Text value given for an Integer field', |
|
'4834':'Invalid ticket. Also check if ticket has expired.', |
|
'4835':'XML parsing error', |
|
'4890':'Wrong API Key', |
|
'4487':'No permission to convert lead.', |
|
'4001':'No API permission', |
|
'401':'No module permission', |
|
'401.1':'No permission to create a record', |
|
'401.2':'No permission to edit a record', |
|
'401.3':'No permission to delete a record', |
|
'4101':'Zoho CRM disabled', |
|
'4102':'No CRM account', |
|
'4103':'No record available with the specified record ID.', |
|
'4422':'No records available in the module', |
|
'4420':'Wrong value for search parameter and/or search parameter value.', |
|
'4421':'Number of API calls exceeded', |
|
'4423':'Exceeded record search limit', |
|
'4807':'Exceeded file size limit', |
|
'4424':'Invalid File Type', |
|
'4809':'Exceeded storage space limit'}; |
|
|
|
} |