Function: Count Related Records in Zoho CRM

11.18.21 11:34 AM By Andy

In this tutorial we are going to create a function that will count the related record of a record, and will add the total count to a field that you can use to filter your records or trigger other automations. For this example, we will count how many Deals are related to a Contact, but you can replace the variables to count other related records, even if they are part of a custom module.


This function will trigger when a new Deal is created. So when you create a new Deal, it will find the contact related to that Deal, and it will count how many Deals the Contact has, and then it will place the total number in a field in the Contact.


If you would like to run this function on all the records you currently have, without having to wait for a related record to be created, you will have to create a different function that will update all the records at once. This tutorial doesn't show how to do that.

Part 1: Add a field to the Contacts layout

  1. In CRM, open the set up page, then go to modules and fields under customization
  2. Open your deals module, in my case it's called opportunities
  3. Open the layout you want to edit
  4. Add a user field and name it
  5. Save and close the module
  6. Then, on the setup, go to developer space and click on APIs
  7. On the top, go to APIs names and open "Deals"
  8. On the list, find the field you just created, and take note of the API name


Part 2: Creating the function

  1. In the set up, click on "functions" under "developer space"
  2. Click on the "new function" button
  3. Name the function and select automation as the category
  4. Copy the code down below and paste it in the function's body
  5. Replace "API_name_field" for the API name that you found on step one (where you want to place the total count number)
  6. Find the API name of the module that you would like to count, in my case it's Deals
  7. Replace "API_name_to_count" with the API name found (Deals)
  8. Find the API name of the main module, in my case it's Contacts
  9. Replace "API_name_record" with the API name found (Contacts)
  10. Now click on "Edit Arguments" and map the argument "contactId" as a "string" (you can change the name of this argument if the main module is different; ie: if you are counting products related to a deal, the main module will be Deal, so you can call this field "DEALID")
  11. Finally save the arguments and save the function
relRecords = zoho.crm.getRelatedRecords("API_name_to_count","API_name_record",contactId.toLong());
mp = Map();
mp.put("API_name_field",relRecords.size().toString());
updateResp = zoho.crm.updateRecord("API_name_record",contactId.toLong(),mp);
info updateResp;

Part 3: Creating the workflow

  1. In the setup page, click on workflow rules under automations
  2. Create a new rule, select the Deals module, and name it (we are selecting the Deals instead of the contact because we need to trigger this automation when a Deal is created; you have to select the module that you are counting)
  3. Configure your workflow trigger and conditions:
    1. When > on a record action > at creation
    2. Condition: all Deals
  4. As the instant action, select "function"
  5. Click on "new function" and then select "functions"
  6. Find the function you just created, and click on "configure"
  7. Map the argument "potId" as the deal ID, using the # key
  8. Click on "save and associate" and save the workflow
  9. Test the workflow