If you've tried searching for ways to add subform rows in Zoho CRM without overwriting existing ones, you've likely stumbled upon API documentation. While APIs work well, they may feel complex, especially if you want to perform this entirely within Zoho CRM using Deluge scripting. Then's a simpler, API-free approach to solve this.
Why Add Subform Rows Dynamically?
Subforms in Zoho CRM act as embedded spreadsheets that let you store multiple line items for a single record. Adding rows dynamically via Deluge scripting can unlock exciting possibilities, such as:
- Activity Logs: Track every outreach on a Lead in a subform called "Outreach Log."
- Product Sales Tracking: Sync subform data from Deals to an Accounts subform, creating a consolidated view of all products sold under the Account.
Step-by-Step Guide to Add Subform Rows Without Overwriting Existing Data
1. Create a Map for the New Row
Start by creating a Map object with keys corresponding to your subform fields and the respective values for the new row.Note: Replace the keys ("Product", "Quantity", etc.) with your subform field API names.
updateMap = Map();
updateMap.put("Product", "Apples");
updateMap.put("Quantity", 50);
updateMap.put("Price", 2);
updateMap = Map();
updateMap.put("Product", "Apples");
updateMap.put("Quantity", 50);
updateMap.put("Price", 2);2. Add the Map to a List
Add the newly created Map into a List.
updateList = List();
updateList.add(updateMap);
updateList = List();updateList.add(updateMap);
3. Preserve Existing Rows
Fetch the existing subform rows to ensure they’re not overwritten. Use the following steps:
- Fetch the record using
zoho.crm.getRecordById. - Get the subform field using its API name.
- Iterate through each subform row and add it to
updateListby including itsid.
record = zoho.crm.getRecordById("Quotes", "683769000000338181"); // Replace with module and record IDsubform = record.get("Quoted_Items"); // Replace with subform API namefor each s in subform {updateList.add({"id": s.get("id")});}
4. Update the Subform
Use the zoho.crm.updateRecord function to update the subform with the complete updateList.
updateCRM = zoho.crm.updateRecord("Quotes", "683769000000338181", {"Quoted_Items": updateList}); info updateCRM;
Scenario: Calculating and Updating Subform Fields
Let’s say you have a subform Quoted_Items with three fields: Quantity, Standard_Price, and Extended_Standard_Price. You want to automatically calculate Extended_Standard_Price as Quantity * Standard_Price and update it for each row.
Here’s the Deluge code to achieve this:
// Replace "quoteId" with your Quote record IDfetch_record = invokeurl [ url: "https://www.zohoapis.in/crm/v6/Quotes/quoteId" type: GET connection: "fetch_data_1" ]; // Parse the JSON response information = fetch_record.get("data"); for each rec in information { quoted_items = rec.get("Quoted_Items"); // Fetch subform data extended_standard_prices = List(); // Process each subform row for each item in quoted_items { subform = Map(); subform.put("id", item.get("id")); // Preserve existing row ID // Fetch and calculate values quantity = ifnull(item.get("Quantity"), 0); standard_price = ifnull(item.get("Standard_Price"), 0); extended_standard_price = quantity * standard_price; // Update the calculated field subform.put("Extended_Standard_Price", extended_standard_price); extended_standard_prices.add(subform); } // Update the CRM record with change subform data main_map = Map(); main_map.put("Quoted_Items", extended_standard_prices); update_resp1 = zoho.crm.updateRecord("Quotes", rec.get("id"), main_map); info update_resp1; }
What’s Next?
You can use this approach to dynamically add, modify, or calculate subform rows in Zoho CRM.