An article with a quick snippet of code which builds up the address from a Lead record to geocode into latitude and longitude coordinates to feed a third-party API which returns a timezone.
Why?
Our use-case is that we have a field called "Customer's Time Zone" on the lead record in ZohoCRM. It should be auto-populated...
How?
As mentioned, we're going to use the address on the Lead record; use Zoho's GeoCode function to convert this into a latitude and longitude, then we're going to use a free TimeZone API (one that returns a timezone given a lat/lng). The added feature is that we are going to loop through picklist options (500 timezones) in CRM to select the most relevant one. If all this fails, then it defaults to a module called "States" which stores a default timezone and can be searched for with the "State" field on the Lead record.
copyraw
//
// ************************************************
// determine customer timezone: BEGIN
//
v_CustomerTimeZone = "";
//
// build up address from Lead to geocode
l_LeadAddress = List();
if(!isnull(r_LeadDetails.get("Street")))
{
l_LeadAddress.add(r_LeadDetails.get("Street"));
}
if(!isnull(r_LeadDetails.get("City")))
{
l_LeadAddress.add(r_LeadDetails.get("City"));
}
if(!isnull(r_LeadDetails.get("State")))
{
l_LeadAddress.add(r_LeadDetails.get("State"));
}
if(!isnull(r_LeadDetails.get("Zip_Code")))
{
l_LeadAddress.add(r_LeadDetails.get("Zip_Code"));
}
if(!isnull(r_LeadDetails.get("Country")))
{
l_LeadAddress.add(r_LeadDetails.get("Country"));
}
//
// use Zoho built-in GeoCode function to return latitude and longitude
r_GeoCode = zoho.map.geoCode(l_LeadAddress.toString(","));
//
// get timezone from a free third-party API given lat/lng
l_Params = List();
if(!isnull(r_GeoCode.get("latitude")))
{
// add user licence key (you'll need to get your own at https://timezonedb.com/references/get-time-zone)
l_Params.add("key=123456789ABCD");
l_Params.add("format=json");
l_Params.add("lat=" + r_GeoCode.get("latitude"));
l_Params.add("lng=" + r_GeoCode.get("longitude"));
l_Params.add("by=position");
}
//
// query with a GET method
r_Timezone = getUrl("http://api.timezonedb.com/v2.1/get-time-zone?" + l_Params.toString("&"));
//
// if we got a response from the third-party API, let's go through the CRM picklist options to find the relevant one
if(!isnull(r_Timezone.get("zoneName")))
{
v_TimeZone = r_Timezone.get("zoneName");
//
// get all the fields on the Lead module
v_Endpoint = "https://www.zohoapis.com/crm/v2/settings/fields?module=Leads";
r_Response = invokeurl
[
url :v_Endpoint
type :GET
connection:"joels_connector"
];
if(!isnull(r_Response.get("fields")))
{
// loop through every field to find the customer's timezone one
for each r_Field in r_Response.get("fields")
{
if(r_Field.get("api_name") == "Customer_s_Timezone")
{
// loop through all the picklist values of this dropdown
for each r_TimeZone in r_Field.get("pick_list_values")
{
if(r_TimeZone.get("display_value").containsIgnoreCase(v_TimeZone))
{
v_CustomerTimeZone = r_TimeZone.get("display_value");
break;
}
}
}
}
}
}
//
// if not blank then we found the correct timezone picklist option
if(v_CustomerTimeZone != "")
{
m_UpdateLead.put("Customer_s_Timezone",v_CustomerTimeZone);
}
//
// if not found, let's see if state is specified
else if(!isnull(r_LeadDetails.get("State")))
{
// find a module record matching the State
l_SearchResults = zoho.crm.searchRecords("States","Name:equals:" + r_LeadDetails.get("State"));
for each r_Result in l_SearchResults
{
if(!isnull(r_Result.get("id")))
{
// get the state record details
r_StateDetails = zoho.crm.getRecordById("States",r_Result.get("id"));
if(!isnull(r_StateDetails.get("TimeZone")))
{
// set to default timezone on state record
m_UpdateLead.put("Customer_s_Timezone",r_StateDetails.get("TimeZone"));
}
}
}
}
//
// ************************************************
// determine customer timezone: END
//
- //
- // ************************************************
- // determine customer timezone: BEGIN
- //
- v_CustomerTimeZone = "";
- //
- // build up address from Lead to geocode
- l_LeadAddress = List();
- if(!isnull(r_LeadDetails.get("Street")))
- {
- l_LeadAddress.add(r_LeadDetails.get("Street"));
- }
- if(!isnull(r_LeadDetails.get("City")))
- {
- l_LeadAddress.add(r_LeadDetails.get("City"));
- }
- if(!isnull(r_LeadDetails.get("State")))
- {
- l_LeadAddress.add(r_LeadDetails.get("State"));
- }
- if(!isnull(r_LeadDetails.get("Zip_Code")))
- {
- l_LeadAddress.add(r_LeadDetails.get("Zip_Code"));
- }
- if(!isnull(r_LeadDetails.get("Country")))
- {
- l_LeadAddress.add(r_LeadDetails.get("Country"));
- }
- //
- // use Zoho built-in GeoCode function to return latitude and longitude
- r_GeoCode = zoho.map.geoCode(l_LeadAddress.toString(","));
- //
- // get timezone from a free third-party API given lat/lng
- l_Params = List();
- if(!isnull(r_GeoCode.get("latitude")))
- {
- // add user licence key (you'll need to get your own at https://timezonedb.com/references/get-time-zone)
- l_Params.add("key=123456789ABCD");
- l_Params.add("format=json");
- l_Params.add("lat=" + r_GeoCode.get("latitude"));
- l_Params.add("lng=" + r_GeoCode.get("longitude"));
- l_Params.add("by=position");
- }
- //
- // query with a GET method
- r_Timezone = getUrl("http://api.timezonedb.com/v2.1/get-time-zone?" + l_Params.toString("&"));
- //
- // if we got a response from the third-party API, let's go through the CRM picklist options to find the relevant one
- if(!isnull(r_Timezone.get("zoneName")))
- {
- v_TimeZone = r_Timezone.get("zoneName");
- //
- // get all the fields on the Lead module
- v_Endpoint = "https://www.zohoapis.com/crm/v2/settings/fields?module=Leads";
- r_Response = invokeUrl
- [
- url :v_Endpoint
- type :GET
- connection:"joels_connector"
- ];
- if(!isnull(r_Response.get("fields")))
- {
- // loop through every field to find the customer's timezone one
- for each r_Field in r_Response.get("fields")
- {
- if(r_Field.get("api_name") == "Customer_s_Timezone")
- {
- // loop through all the picklist values of this dropdown
- for each r_TimeZone in r_Field.get("pick_list_values")
- {
- if(r_TimeZone.get("display_value").containsIgnoreCase(v_TimeZone))
- {
- v_CustomerTimeZone = r_TimeZone.get("display_value");
- break;
- }
- }
- }
- }
- }
- }
- //
- // if not blank then we found the correct timezone picklist option
- if(v_CustomerTimeZone != "")
- {
- m_UpdateLead.put("Customer_s_Timezone",v_CustomerTimeZone);
- }
- //
- // if not found, let's see if state is specified
- else if(!isnull(r_LeadDetails.get("State")))
- {
- // find a module record matching the State
- l_SearchResults = zoho.crm.searchRecords("States","Name:equals:" + r_LeadDetails.get("State"));
- for each r_Result in l_SearchResults
- {
- if(!isnull(r_Result.get("id")))
- {
- // get the state record details
- r_StateDetails = zoho.crm.getRecordById("States",r_Result.get("id"));
- if(!isnull(r_StateDetails.get("TimeZone")))
- {
- // set to default timezone on state record
- m_UpdateLead.put("Customer_s_Timezone",r_StateDetails.get("TimeZone"));
- }
- }
- }
- }
- //
- // ************************************************
- // determine customer timezone: END
- //
Source(s):
Category: Zoho :: Article: 792



Add comment