For Zoho Services only:


I'm actually part of a bigger team at Ascent Business Solutions where we have support technicians and project consultants. Support is for smaller technical fixes but this can include developments, reports or integrations; depending on the size of the task. Projects are for more time-consuming developments such as revamps of the Zoho Suite of apps or on-site training. The advantage of a team is that if I am out-of-office for a day or so, there is always someone at Ascent Business Solutions who can deal with any queries/issues you may have.

Our support rates can be found and purchased at http://ascentbusiness.co.uk/zoho-support-2. A support bundle doesn't have an expiry date. So whether we can do what you want within the bundle and a year later need further support, if there are minutes left on the bundle then there is no additional charge.

Our project rates for bigger developments can be found at http://ascentbusiness.co.uk/crm-solutions/zoho-crm-packages-prices and will involve a dedicated project consultant along with developers who will hold your hand through the development process.

If you want help building a solution for one of the Zoho Apps in the Zoho Suite, contact us on 0121 293 8140 (UK) or by email at info@ascentbusiness.co.uk. You can also visit our website at http://ascentbusiness.co.uk.

I regularly build and specialize in 2-way API integrations for Xero, Shopify and eBay.

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: The information on this website is provided without warranty and any content is merely the opinion of the author. Please try to test in development environments prior to adapting them to your production environments. The articles are written in good faith and, at the time of print, are working examples used 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 - Valid till 8 May 2022 3QnhmaBX7LQSRsC9hh6Je9rGQKEGNQNfPb
© 2022 Joel Lipman .com. All Rights Reserved.