Sum Total of various fields in child module and add value to parent module field

Sum Total of various fields in child module and add value to parent module field

Hi! Having trouble with a custom function, im trying to calculate the total of all the rent and sqm fields of our offices in Products module and have them transfer to the parent module Location.

The API names are as below:

Child module
Products = "Products"
Currency Field: Rent Achieved = "Rent_Achieved"
Number Field: SQM = "Square_Meterage"

Parent module
Locations = "Locations"
Currency Field: Total Rent Achieved = "Total_Rent_Achieved"
Number Field: Occupied Area (sqm) = "Occupied_Area_sqm"


I wrote this out but its not working, can anyone help?


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;

    }

}