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;

    }

}


    • Sticky Posts

    • Kaizen #198: Using Client Script for Custom Validation in Blueprint

      Nearing 200th Kaizen Post – 1 More to the Big Two-Oh-Oh! Do you have any questions, suggestions, or topics you would like us to cover in future posts? Your insights and suggestions help us shape future content and make this series better for everyone.
    • Kaizen #226: Using ZRC in Client Script

      Hello everyone! Welcome to another week of Kaizen. In today's post, lets see what is ZRC (Zoho Request Client) and how we can use ZRC methods in Client Script to get inputs from a Salesperson and update the Lead status with a single button click. In this
    • Kaizen #222 - Client Script Support for Notes Related List

      Hello everyone! Welcome to another week of Kaizen. The final Kaizen post of the year 2025 is here! With the new Client Script support for the Notes Related List, you can validate, enrich, and manage notes across modules. In this post, we’ll explore how
    • Kaizen #217 - Actions APIs : Tasks

      Welcome to another week of Kaizen! In last week's post we discussed Email Notifications APIs which act as the link between your Workflow automations and you. We have discussed how Zylker Cloud Services uses Email Notifications API in their custom dashboard.
    • Kaizen #216 - Actions APIs : Email Notifications

      Welcome to another week of Kaizen! For the last three weeks, we have been discussing Zylker's workflows. We successfully updated a dormant workflow, built a new one from the ground up and more. But our work is not finished—these automated processes are