For Zoho Services only:


I'm actually part of something bigger at Ascent Business Solutions recognized as the top Zoho Premium Solutions Partner in the United Kingdom.

Ascent Business Solutions offer support for smaller technical fixes and projects for larger developments, such as migrating to a ZohoCRM.  A team rather than a one-man-band is always available to ensure seamless progress and address any concerns. You'll find our competitive support rates with flexible, no-expiration bundles at http://ascentbusiness.co.uk/zoho-support-2.  For larger projects, check our bespoke pricing structure and receive dedicated support from our hands-on project consultants and developers at http://ascentbusiness.co.uk/crm-solutions/zoho-crm-packages-prices.

The team I manage specializes in coding API integrations between Zoho and third-party finance/commerce suites such as Xero, Shopify, WooCommerce, and eBay; to name but a few.  Our passion lies in creating innovative solutions where others have fallen short as well as working with new businesses, new sectors, and new ideas.  Our success is measured by the growth and ROI we deliver for clients, such as transforming a garden shed hobby into a 250k monthly turnover operation or generating a +60% return in just three days after launch through online payments and a streamlined e-commerce solution, replacing a paper-based system.

If you're looking for a partner who can help you drive growth and success, we'd love to work with you.  You can reach out to us on 0121 392 8140 (UK) or info@ascentbusiness.co.uk.  You can also visit our website at http://ascentbusiness.co.uk.

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

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

Related Articles

Joes Revolver Map

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin bc1qf6elrdxc968h0k673l2djc9wrpazhqtxw8qqp4

Ethereum:
Donate to Joel Lipman with Ethereum 0xb038962F3809b425D661EF5D22294Cf45E02FebF
© 2024 Joel Lipman .com. All Rights Reserved.