Zoho Flow Custom Function - Collection Error

Zoho Flow Custom Function - Collection Error

I have setup a custom function that takes a string value I receive from a webhook which is the full address provided by a user.

I want to break this address into it's pieces so as to map each piece to Zoho CRM fields (ie. street, city etc).

This function worked fine before I added in logic to loop over the collection to check if specific values are there and map them accordingly (ie. check which element in the collection is the country and then map to country output).

Without the if statement logic things work fine.  With the if statement logic I am getting an error about an invalid collection.

Here is the function:

  1. map FlexiPerk_Location_AddressSplit(string location)
  2. {
  3.     // Initialize the output values to empty strings
  4.     street = "";
  5.     suburb = "";
  6.     city = "";
  7.     province = "";
  8.     country = "";
  9.     postalCode = "";
  10.     
  11.     // Define the list of provinces and countries
  12.     provinces = ["Eastern Cape", "Free State", "Gauteng", "KwaZulu-Natal", "Limpopo", "Mpumalanga", "North West", "Northern Cape", "Western Cape"];
  13.     countries = ["South Africa"];
  14.     
  15.     // Split the location into its components
  16.     components = location.toListString();
  17.     info components;
  18.        
  19.     // Check the length of the components list
  20.     if (components.length == 1) {
  21.         street = components.get(0);
  22.     } else {
  23.         // Loop through the components and check for the province, country, and postal code components
  24.         for each locationElement in components {
  25.             if (provinces.contains(locationElement)) {
  26.                 province = locationElement;
  27.             } else if (countries.contains(locationElement)) {
  28.                 country = locationElement;
  29.             } else if (locationElement.length == 4) {
  30.                 postalCode = locationElement;
  31.             } else {
  32.                 // If the component is not the province, country, or postal code, add it to the street value
  33.                 if (street != "") {
  34.                     street += ", ";
  35.                 }
  36.                 street += locationElement;
  37.             }
  38.         }
  39.     }
  40.     info street;
  41.     info suburb;
  42.     info city;
  43.     info province;
  44.     info country;
  45.     info postalCode;
  46.     // Create a map to store the output values
  47.     outputMap = Map();
  48.     // Assign the output values to the output map
  49.     outputMap.put("street", street);
  50.     outputMap.put("suburb", suburb);
  51.     outputMap.put("city", city);
  52.     outputMap.put("province", province);
  53.     outputMap.put("country", country);
  54.     outputMap.put("postalCode", postalCode);
  55.     
  56.     output = outputMap;
  57.     return output;
  58. }


Any idea how to fix?