Last active
August 17, 2022 12:22
-
-
Save cliffordp/132f1940b6d90a31db60faae0e20dc5f to your computer and use it in GitHub Desktop.
Zoho Flow custom function for Zoho CRM - Lookup full address (USA terminology)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
map gmapsSingleAddressLookup(string fullAddress) | |
{ | |
/** | |
* | |
* This snippet: https://gist.github.com/cliffordp/132f1940b6d90a31db60faae0e20dc5f | |
* | |
* Get lat/long and other details from Google. | |
* https://developers.google.com/maps/documentation/geocoding | |
* | |
* CRM limits Decimal Fields to 9 digits, which is sufficient: | |
* https://rapidlasso.com/2019/05/06/how-many-decimal-digits-for-storing-longitude-latitude/ | |
* | |
* Deluge has its own geocoder available, but they say it's likely not as accurate as a commercial service: | |
* https://www.zoho.com/deluge/help/map-tasks/geocode.html | |
*/ | |
apiKey = zoho.crm.getOrgVariable("googleMapsApiKeyApiName"); | |
//info apiKey; | |
fullAddress = trim(fullAddress); | |
if(isBlank(apiKey) || isBlank(fullAddress)) | |
{ | |
return null; | |
} | |
// API key from CRM variable. USA region. Suggest Oklahoma bounds (without panhandle). The address we're looking up | |
geoUrl = 'https://maps.googleapis.com/maps/api/geocode/json?key=' + apiKey + '®ion=us' + '&bounds=' + zoho.encryption.urlencode('37.169071,-100.206985|33.385586,-93.966751') + '&address=' + zoho.encryption.urlencode(fullAddress); | |
//info geoUrl; | |
// https://www.zoho.com/deluge/help/webhook/invokeurl-api-task.html | |
response = invokeurl | |
[ | |
url :geoUrl | |
type :POST | |
]; | |
//info response; | |
// Bail if bad response. | |
valueMap = {"attemptedAddress":fullAddress,"rawGoogleResponse":response}; | |
info valueMap; | |
if(response.contains("error_message")) | |
{ | |
sendmail | |
[ | |
from :zoho.adminuserid | |
to :"EXAMPLE@EXAMPLE.COM" | |
subject :"OKCrete Geocoding Error" | |
message :valueMap | |
] | |
return valueMap; | |
} | |
results = response.get('results').get(0); | |
addressComponents = results.get('address_components'); | |
// Bail if bad response or if it is for a general area (City, State, Zip) because we only want a single address. | |
// We don't use `"true" == results.get("partial_match")` because entering a place name might find its full address. | |
if(response.contains("error_message") || 'OK' != response.get('status') || addressComponents.notContains('street_number')) | |
{ | |
sendmail | |
[ | |
from :zoho.adminuserid | |
to :"EXAMPLE@EXAMPLE.COM" | |
subject :"EXAMPLE ORG Geocoding Error" | |
message :valueMap | |
] | |
return valueMap; | |
} | |
// Continue if OK response... Get first result (should only be 1 result anyway). | |
for each item in addressComponents | |
{ | |
type = item.get('types').get(0); | |
// Replace Google's keys with easier to recognize ones. | |
if('locality' == type) | |
{ | |
type = 'city'; | |
} | |
else if('administrative_area_level_2' == type) | |
{ | |
type = 'county'; | |
} | |
else if('administrative_area_level_1' == type) | |
{ | |
type = 'state'; | |
} | |
else if('postal_code' == type) | |
{ | |
type = 'zip'; | |
} | |
valueMap.put(type,item.get('short_name')); | |
} | |
//info valueMap; | |
// CRM does not have an Address Line 2 so we combine all items (street#, route, apt#) into 'line1'. | |
if(isNull(valueMap.get("subpremise"))) | |
{ | |
// subpremise is the Apt#. isBlank() does not work, ugh. | |
valueMap.put('line1',valueMap.get('street_number') + ' ' + valueMap.get('route')); | |
} | |
else | |
{ | |
valueMap.put('line1',valueMap.get('street_number') + ' ' + valueMap.get('route') + ' #' + valueMap.get('subpremise')); | |
} | |
valueMap.put('fullAddress',replaceFirst(results.get('formatted_address'),', USA','')); | |
valueMap.put('lat',round(results.get('geometry').get('location').get('lat').toDecimal(),9)); | |
valueMap.put('lng',round(results.get('geometry').get('location').get('lng').toDecimal(),9)); | |
//info valueMap; | |
// In Zoho Flow, do something like this: ${gmapsAddressLookup.line1} | |
return valueMap; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment