Looping Through Account Contacts to Get Location Distances
NOTE
Calculating distances to other locations is a paid feature and not available in the free version of the extension. If you are not on a paid plan then the location data is ignored and no information regarding locations will be returned.
You can switch to a paid plan via the settings of the extension in Zoho CRM.
In this example, let's say that you have an account for your company called "My Company" and all the contacts are the office locations for your company, with each contact name representing the sales person and the department representing the branch name.
You want to loop over all the offices and add them to your query so you can calculate the distances between the source zip/postal code and each office. With the result we'll then update our lead record with the nearest office and assign it to the correct sales person.
Deluge Code
// Get all the office locations for My Company
company_name = "My Company";
company_account = zoho.crm.searchRecords("Accounts","(Account_Name:equals:" + company_name + ")");
offices = zoho.crm.getRelatedRecords("Contacts", "Accounts", company_account.get(0).get("id") );
locations = List();
for each office in offices
{
location = Map();
location.put("name", office.get("Department"));
location.put("zip_postal", office.get("Mailing_Zip"));
locations.add(location);
}
Location Results List
[
{
"name": "Toronto Office",
"salesperson_first": "James",
"salesperson_last": "Duncan",
"zip_postal": "M6M 1K6"
},
{
"name": "Boulder Office",
"salesperson_first": "Samantha",
"salesperson_last": "Brooks",
"zip_postal": "80307"
},
{
"name": "Beverly Hills Office",
"salesperson_first": "Tim",
"salesperson_last": "Smith",
"zip_postal": "90210"
}
]
Deluge Code To Perform The Lookup
// Record type and ID passed from the workflow into a custom automation function
void lookupContactZipChanged(String recordType, Int recordId, String postalCode) {
fields = Map();
fields.put("Mailing_City","base_city");
fields.put("Mailing_State","base_state_province");
fields.put("Mailing_Zip","base_postal_zip");
fields.put("Mailing_Country","country_name");
fields.put("Closest_Office_Distance","distance_1_miles");
fields.put("Closest_Office","distance_1_name");
fields.put("Closest_Office_Rep","distance_1_salesperson_first");
conditions = Map();
conditions.put("Mailing_City","onlyifempty");
conditions.put("Mailing_State","onlyifempty");
conditions.put("Mailing_Country","onlyifempty");
// Get all the office locations for My Company
company_name = "My Company";
company_account = zoho.crm.searchRecords("Accounts","(Account_Name:equals:" + company_name + ")");
offices = zoho.crm.getRelatedRecords("Contacts", "Accounts", company_account.get(0).get("id") );
locations = List();
for each office in offices
{
location = Map();
location.put("name", office.get("Department"));
location.put("salesperson_first", office.get("First_Name"));
location.put("salesperson_last", office.get("Last_Name"));
location.put("zip_postal", office.get("Mailing_Zip"));
locations.add(location);
}
distances = Map();
distances.put("max_distance", 100);
distances.put("max_count", 3);
distances.put("locations", locations);
addressautoresolver.lookupAndParsePostalCode(recordType,recordId,postalCode,fields,conditions,distances);
}
Workflow