Skip to content

Instantly share code, notes, and snippets.

@cliffordp
Last active November 21, 2023 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cliffordp/2654cda30ecd0029b1e617a0f7a0c6c9 to your computer and use it in GitHub Desktop.
Save cliffordp/2654cda30ecd0029b1e617a0f7a0c6c9 to your computer and use it in GitHub Desktop.
Zoho Flow custom function using Google Maps for address validation
map addressValidation(string addressString)
{
/**
* This snippet: https://gist.github.com/cliffordp/2654cda30ecd0029b1e617a0f7a0c6c9
*
* Pass each address parameter (address1, address2, city, state, zip, country) that you have in a comma-separated string,
* such as "1234 Easy Street, 55533".
*
* Will return a results map. Examples of what you could use in Zoho Flow:
* ${addressValidation.formattedAddress} is from API but may not include all address pieces (depends on input pieces)
* ${addressValidation.fullAddressFormatted} is better to use than the above except that it's in all caps. It is made up of:
* firstAddressLine secondAddressLine, cityStateZipAddressLine (no space before comma if empty secondAddressLine)
* ${addressValidation.firstAddressLine}
* ${addressValidation.secondAddressLine}
* ${addressValidation.cityStateZipAddressLine}
* ${addressValidation.city}
* ${addressValidation.state}
* ${addressValidation.zipCode}
* ${addressValidation.zipCodeExtension}
*
* @link https://share.zight.com/Z4upBK40 Example output.
* @link https://developers.google.com/maps/documentation/address-validation/
*/
apiKey = '';
apiUrl = ('https://addressvalidation.googleapis.com/v1:validateAddress?key=' + apiKey);
payload = {"address":{"regionCode":"US","addressLines":{addressString}}};
headers = Map();
headers.put("Content-Type","application/json");
// Send the request
response = postUrl(apiUrl,payload.toString(),headers);
info response;
responseMap = response.toCollection();
resultMap = Map();
if(responseMap.containsKey("result"))
{
result = responseMap.get("result");
// Checking for 'formattedAddress'.
if(result.containsKey("address"))
{
address = result.get("address");
if(address.containsKey("formattedAddress"))
{
resultMap.put("formattedAddress",address.get("formattedAddress"));
}
else
{
// Bail if no formattedAddress.
return resultMap;
}
}
// Extracting 'standardizedAddress' keys.
// @link https://developers.google.com/maps/documentation/address-validation/reference/rest/v1/TopLevel/validateAddress#uspsaddress
if(result.containsKey("uspsData"))
{
uspsData = result.get("uspsData");
if(uspsData.containsKey("standardizedAddress"))
{
standardizedAddress = uspsData.get("standardizedAddress");
for each key in standardizedAddress.keys()
{
resultMap.put(key,standardizedAddress.get(key));
}
}
}
// Extracting 'standardizedAddress' and constructing 'fullAddressFormatted'
if(result.containsKey("uspsData"))
{
uspsData = result.get("uspsData");
if(uspsData.containsKey("standardizedAddress"))
{
standardizedAddress = uspsData.get("standardizedAddress");
fullAddressFormatted = "";
// Constructing fullAddressFormatted
if(standardizedAddress.containsKey("firstAddressLine"))
{
fullAddressFormatted = fullAddressFormatted + standardizedAddress.get("firstAddressLine");
}
if(standardizedAddress.containsKey("secondAddressLine"))
{
fullAddressFormatted = fullAddressFormatted + " " + standardizedAddress.get("secondAddressLine");
}
if(standardizedAddress.containsKey("cityStateZipAddressLine") && fullAddressFormatted != "")
{
fullAddressFormatted = fullAddressFormatted + ", ";
fullAddressFormatted = fullAddressFormatted + standardizedAddress.get("cityStateZipAddressLine");
}
resultMap.put("fullAddressFormatted",fullAddressFormatted);
// Adding other standardizedAddress components
for each key in standardizedAddress.keys()
{
resultMap.put(key,standardizedAddress.get(key));
}
}
}
}
return resultMap;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment