Print

Zoho CRM/Deluge: Get TimeZone Based on GeoCoded Address (Lat/Lng)

What?
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
//
  1.  // 
  2.  // ************************************************ 
  3.  // determine customer timezone: BEGIN 
  4.  // 
  5.  v_CustomerTimeZone = ""
  6.  // 
  7.  // build up address from Lead to geocode 
  8.  l_LeadAddress = List()
  9.  if(!isnull(r_LeadDetails.get("Street"))) 
  10.  { 
  11.      l_LeadAddress.add(r_LeadDetails.get("Street"))
  12.  } 
  13.  if(!isnull(r_LeadDetails.get("City"))) 
  14.  { 
  15.      l_LeadAddress.add(r_LeadDetails.get("City"))
  16.  } 
  17.  if(!isnull(r_LeadDetails.get("State"))) 
  18.  { 
  19.      l_LeadAddress.add(r_LeadDetails.get("State"))
  20.  } 
  21.  if(!isnull(r_LeadDetails.get("Zip_Code"))) 
  22.  { 
  23.      l_LeadAddress.add(r_LeadDetails.get("Zip_Code"))
  24.  } 
  25.  if(!isnull(r_LeadDetails.get("Country"))) 
  26.  { 
  27.      l_LeadAddress.add(r_LeadDetails.get("Country"))
  28.  } 
  29.  // 
  30.  // use Zoho built-in GeoCode function to return latitude and longitude 
  31.  r_GeoCode = zoho.map.geoCode(l_LeadAddress.toString(","))
  32.  // 
  33.  // get timezone from a free third-party API given lat/lng 
  34.  l_Params = List()
  35.  if(!isnull(r_GeoCode.get("latitude"))) 
  36.  { 
  37.      // add user licence key (you'll need to get your own at https://timezonedb.com/references/get-time-zone) 
  38.      l_Params.add("key=123456789ABCD")
  39.      l_Params.add("format=json")
  40.      l_Params.add("lat=" + r_GeoCode.get("latitude"))
  41.      l_Params.add("lng=" + r_GeoCode.get("longitude"))
  42.      l_Params.add("by=position")
  43.  } 
  44.  // 
  45.  // query with a GET method 
  46.  r_Timezone = getUrl("http://api.timezonedb.com/v2.1/get-time-zone?" + l_Params.toString("&"))
  47.  // 
  48.  // if we got a response from the third-party API, let's go through the CRM picklist options to find the relevant one 
  49.  if(!isnull(r_Timezone.get("zoneName"))) 
  50.  { 
  51.      v_TimeZone = r_Timezone.get("zoneName")
  52.      // 
  53.      // get all the fields on the Lead module 
  54.      v_Endpoint = "https://www.zohoapis.com/crm/v2/settings/fields?module=Leads"
  55.      r_Response = invokeUrl 
  56.      [ 
  57.          url :v_Endpoint 
  58.          type :GET 
  59.          connection:"joels_connector" 
  60.      ]
  61.      if(!isnull(r_Response.get("fields"))) 
  62.      { 
  63.          // loop through every field to find the customer's timezone one 
  64.          for each  r_Field in r_Response.get("fields") 
  65.          { 
  66.              if(r_Field.get("api_name") == "Customer_s_Timezone") 
  67.              { 
  68.                  // loop through all the picklist values of this dropdown 
  69.                  for each  r_TimeZone in r_Field.get("pick_list_values") 
  70.                  { 
  71.                      if(r_TimeZone.get("display_value").containsIgnoreCase(v_TimeZone)) 
  72.                      { 
  73.                          v_CustomerTimeZone = r_TimeZone.get("display_value")
  74.                          break
  75.                      } 
  76.                  } 
  77.              } 
  78.          } 
  79.      } 
  80.  } 
  81.  // 
  82.  // if not blank then we found the correct timezone picklist option 
  83.  if(v_CustomerTimeZone != "") 
  84.  { 
  85.      m_UpdateLead.put("Customer_s_Timezone",v_CustomerTimeZone)
  86.  } 
  87.  // 
  88.  // if not found, let's see if state is specified 
  89.  else if(!isnull(r_LeadDetails.get("State"))) 
  90.  { 
  91.      // find a module record matching the State 
  92.      l_SearchResults = zoho.crm.searchRecords("States","Name:equals:" + r_LeadDetails.get("State"))
  93.      for each  r_Result in l_SearchResults 
  94.      { 
  95.          if(!isnull(r_Result.get("id"))) 
  96.          { 
  97.              // get the state record details 
  98.              r_StateDetails = zoho.crm.getRecordById("States",r_Result.get("id"))
  99.              if(!isnull(r_StateDetails.get("TimeZone"))) 
  100.              { 
  101.                  // set to default timezone on state record 
  102.                  m_UpdateLead.put("Customer_s_Timezone",r_StateDetails.get("TimeZone"))
  103.              } 
  104.          } 
  105.      } 
  106.  } 
  107.  // 
  108.  // ************************************************ 
  109.  // determine customer timezone: END 
  110.  // 

Source(s):
Category: Zoho :: Article: 792