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
- In CRM, open the set up page, then go to modules and fields under customization
- Open your deals module, in my case it's called opportunities
- Open the layout you want to edit
- Add a user field and name it
- Save and close the module
- Then, on the setup, go to developer space and click on APIs
- On the top, go to APIs names and open "Deals"
- On the list, find the field you just created, and take note of the API name
Part 2: Creating the function
- In the set up, click on "functions" under "developer space"
- Click on the "new function" button
- Name the function and select automation as the category
- Copy the code down below and paste it in the function's body
- Replace "API_name_field" for the API name that you found on step one (where you want to place the total count number)
- Find the API name of the module that you would like to count, in my case it's Deals
- Replace "API_name_to_count" with the API name found (Deals)
- Find the API name of the main module, in my case it's Contacts
- Replace "API_name_record" with the API name found (Contacts)
- 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")
- Finally save the arguments and save the function
Part 3: Creating the workflow
- In the setup page, click on workflow rules under automations
- 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)
- Configure your workflow trigger and conditions:
- When > on a record action > at creation
- Condition: all Deals
- As the instant action, select "function"
- Click on "new function" and then select "functions"
- Find the function you just created, and click on "configure"
- Map the argument "potId" as the deal ID, using the # key
- Click on "save and associate" and save the workflow
- Test the workflow