When managing customer relationships, tracking the number of deals created for a contact is essential because it can provide valuable insights into the level of engagement between your business and the contact. A higher number of deals indicates greater interest in your products or services, while a lower number may suggest there's a need for additional outreach and relationship-building efforts. Additionally, recording the deal count can help you filter contacts or use them in process automations. However, there's no specific field in CRM that records the number of deals created or closed for a contact. This is where a custom function can come in handy.
Using a custom function, you can add the number of deals created for a contact within the last 12 months and update this count into a custom number field in the contact record. This function can then be connected to a workflow rule that triggers when a contact record is created or edited.
Permissions and availability
- Users with the Modules Customization permission can add custom fields.
- Users with the Manage Automation permission can create and update workflow rules.
- Users with the Manage Extensibility permission can create custom functions.
- Users with the Manage Sandbox permission can manage the sandbox.
Requirements
- Add a number field in the Contacts module. This field will hold the number of deals created for a contact. Learn more
- Create a workflow rule for the Contacts module that triggers every time a contact is created or edited. Learn more
- Write a custom Deluge function and link it up with the workflow rule. Learn more
- Test the workflow rule in a sandbox before deploying it to your production environment. Learn more
Add a custom field
The first step involves adding the custom number field to the Contacts module.
- Navigate to Setup > Customization > Modules and Fields.
- Click the Contacts module to open the layout editor.
- Drag and drop the Number field from the New Fields tray to the desired section of the layout.
- Name the number field (e.g. "Deal Count") and define its properties as required.
- Once you have finished, click Save and Close.
Create a workflow rule
Follow the below steps to create a workflow rule that updates the deal count in contacts.
- Navigate to Setup > Workflow Rules > +Create Rule.
- Select Contacts from the module dropdown list.
- Provide a name for the rule. For example: "Update deal count in contact records".
- Add a description (optional) and then click Next.
- In the Execute this workflow rule based on section, do the following:
- Choose Record Action, then select Create or Edit from the dropdown.
- Check the option "Repeat this workflow whenever a contact is edited".
- Click Next.
- In the Which contacts would you like to apply the rule to? section, select All Contacts, then click Next.
- Under Instant Actions, select Function and Write your own.
- Provide a name and description, if necessary, for the function.
- On the Deluge Script Editor, do the following:
- Copy and paste the code given below.
- Click Edit Arguments.
- Choose Contacts - Contact Id, name it contId, then click Save.
- Click Save & Execute Script.
- Click Save on the workflow rule page.
The code
relateddeals = zoho.crm.getRelatedRecords("Deals","Contacts",contId.toLong(),1);
count = 0;
if(relateddeals.size() != 0)
{
for each deal in relateddeals
{
dealdate = deal.get("Created_Time").toDate().toStartOfMonth();
currentdate = today.toDate().toStartOfMonth();
monthdiff = monthsBetween(dealdate,currentdate);
info monthdiff;
if(monthdiff <= 12)
{
count = count + 1;
}
}
}
mp = Map();
mp.put("Deal_Count",count);
update = zoho.crm.updateRecord("Contacts",contId.toLong(),mp);
info mp;
info update;
Note
- Make sure to use the accurate API names for their corresponding fields (e.g., "Deal Count") in the code snippet. Learn more
- The code above is an example of adding the number of deals created for a contact within the last 12 months in one of its custom number fields. You can use this code for any other module, such as a custom module, by modifying the module name and parameters.
Tip
- Configure and test the function in a sandbox to ensure that further development doesn't disrupt your production environment.
Test the solution
- Navigate to the Contacts module.
- Select a contact record that has one or more deals linked to it.
Ensure it has at least one deal created within the last 12 months. - On the Contact Details page, edit any field property and save.
The workflow rule will be triggered. - Check whether the Deal Count field on the contact was updated with the accurate number of deals linked to the contact.
Idea
The code above computes all the deals created for a contact over the last 12 months. What if you only need to add those that are closed-won or closed-lost? You can add an extra line of code to filter them out.
relateddeals = zoho.crm.getRelatedRecords("Deals", "Contacts", contId.toLong(),1);
count = 0;
if( relateddeals.size() != 0)
{
for each deal in relateddeals
{
if(deal.get("Stage")=="Closed Won")
{
dealdate = deal.get("Created_Time").toDate().toStartOfMonth();
currentdate = today.toDate().toStartOfMonth();
monthdiff = monthsBetween(dealdate, currentdate);
info monthdiff;
if( monthdiff <= 12)
{
count = count + 1;
}
}
}
}
mp=map();
mp.put("Deal_Count",count);
update = zoho.crm.updateRecord("Contacts", contId.toLong(), mp);
info mp;
info update;
Did you find this useful? Try it out and let us know how it works. Share this with your team if they'd benefit from it! If you have questions, please don't hesitate to contact us.
More info
- ModuleContacts
- Trigger PointWorkflow Rule
- EditionEnterprise and above
- ComplexityMedium
- Implementation Time30 minutes

Looking for a custom solution?
Contact us, we will help enhance your productivity at lightning speed.
SUBMIT REQUEST