Skip to content

Instantly share code, notes, and snippets.

@jeznag
Last active September 4, 2022 10:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeznag/97c5bf30f1f54897383d5a8837d747b1 to your computer and use it in GitHub Desktop.
Save jeznag/97c5bf30f1f54897383d5a8837d747b1 to your computer and use it in GitHub Desktop.
Geocode a Zoho CRM lead using a node js custom function
const axios = require('axios')
const fs = require('fs')
const POSITION_STACK_API_KEY = 'REDACTED';
module.exports = async function (context, basicIO) {
/* 1)entity_object 2)user_object 3)organization_object 4)variables_object 5)request_object are the default parameters which contains entity information, user information,
organization information, variables inforamtion and request information in it respectively.
2) For Button Mass action and RL Button, ids list will be available in basicIO object. you can get by basicIO.getParameter("ids") & basicIO.getParameter("related_entity_ids") respectively
*/
// To get the Entity Information
const entityObject = basicIO.getParameter("entity_object");
const entityId = entityObject.id; // Id of the record which triggered the workflow rule
const connector = context.getConnection("zcrm_all");
async function getRecordData (moduleName, entityId) {
const connector = context.getConnection("zcrm_all");
const connRes = await connector.makeRequestSync({
url: `https://www.zohoapis.com/crm/v2.1/${moduleName}/${entityId}`,
method: "GET"
});
let result;
connRes.on("data", function (data) {
result = data;
})
return new Promise(function (resolve, reject) {
connRes.on("error", function (error) {
resolve("Error");
});
connRes.on("end", () => {
resolve(JSON.parse(result));
});
});
}
const { data: [ recordData] } = await getRecordData('Leads', entityId);
throw new Error(`Record Data: ${JSON.stringify(recordData)}`)
const address = `${recordData.Street || ''} ${recordData.City || ''} ${recordData.State || ''} ${recordData.Zip || ''} ${recordData.Country || ''}`;
const url = `http://api.positionstack.com/v1/forward?query=${address}&access_key=${POSITION_STACK_API_KEY}`;
const { data } = await axios.get(url);
basicIO.write(address);
context.log.INFO(data);
const matches = data.data;
const [{
latitude,
longitude,
label: formattedAddress
}] = matches
context.log.INFO(latitude, longitude);
const updatePayload = {
"data": [{
"Latitude": latitude.toString(),
"Longitude": longitude.toString(),
"Street_name_used_for_geocoding": formattedAddress
}],
"trigger": [
"approval",
"workflow",
"blueprint"
]
};
async function updateRecord(moduleName, entityId, updateData) {
const connector = context.getConnection("zcrm_all");
const connRes = await connector.makeRequestSync({
url: `https://crm.zoho.com/crm/v2/${moduleName}/${entityId}`,
method: "PUT"
}, JSON.stringify(updateData));
let result;
connRes.on("data", function (data) {
result = data;
})
return new Promise(function (resolve, reject) {
connRes.on("error", function (error) {
throw new Error(`Record Data: ${error}`)
resolve("Error");
});
connRes.on("end", () => {
resolve(JSON.parse(result));
});
});
}
const updateResult = await updateRecord('Leads', entityId, updatePayload);
context.log.INFO("log data");
context.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment