void automation.SumTotalsRentandArea(string locationId)
{
// 1. Search for related Products using the Location ID
criteria = "(Location.id:equals:" + locationId + ")";
products = zoho.crm.searchRecords("Products", criteria);
// 2. Initialize totals
totalRent = 0.0; // Initialize rent total as a currency
totalArea = 0.0; // Initialize area total as a number
// 3. Loop through and sum values for each related Product
if (products != null && products.size() > 0)
{
for each product in products
{
// Retrieve Rent_Achieved (Currency) and Square_Meterage (Number)
rent = product.get("Rent_Achieved");
sqm = product.get("Square_Meterage");
// Safely handle Rent_Achieved field (Currency type)
if (rent != null)
{
try
{
rent = rent.toDecimal(); // Convert to Decimal for currency field
}
catch (e)
{
rent = 0.0; // Set to 0 if there is an error in conversion
}
}
else
{
rent = 0.0;
}
// Safely handle Square_Meterage field (Number type)
if (sqm != null)
{
try
{
sqm = sqm.toDecimal(); // Convert to Decimal for area (if needed)
}
catch (e)
{
sqm = 0.0; // Set to 0 if there is an error in conversion
}
}
else
{
sqm = 0.0;
}
// Add rent and area to total values
totalRent += rent;
totalArea += sqm;
}
// 4. Update the Location record with the total rent and area values
updateMap = map();
updateMap.put("Total_Rent_Achieved", totalRent); // Currency field
updateMap.put("Occupied_Area_sqm", totalArea); // Number field
updateResponse = zoho.crm.updateRecord("Locations", locationId, updateMap);
info "Updated Location: " + locationId + " | Total Rent Achieved: " + totalRent + " | Occupied Area sqm: " + totalArea;
}
else
{
info "No related Products found for Location ID: " + locationId;
}
}