For Zoho services only


I'm currently part of a wider delivery team at Ascent Business Solutions, recognised as a leading Zoho Premium Solutions Partner in the United Kingdom.

Ascent Business Solutions support organisations with everything from targeted technical fixes through to full Zoho CRM implementations and long-term platform adoption. Working as a team rather than a one-person consultancy allows projects to move forward consistently, with access to the right skills at each stage.

The team I manage specialises in API integrations between Zoho and third-party finance and commerce platforms such as Xero, Shopify, WooCommerce, and eBay. Much of our work involves solving integration challenges that fall outside standard documentation, supporting new ideas, new sectors, and evolving business models.

Success is measured through practical outcomes and return on investment, ranging from scaling small operations into high-turnover businesses to delivering rapid gains through online payments, automation, and streamlined digital workflows.

If you are looking for structured Zoho expertise backed by an established consultancy, you can contact Ascent Business Solutions on 0121 392 8140 (UK), email info@ascentbusiness.co.uk, or visit https://www.ascentbusiness.co.uk.
Zoho CRM/Deluge: Get TimeZone Based on GeoCoded Address (Lat/Lng)

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 CRM :: Article: 327

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com