For Zoho Services only:


I'm actually part of something bigger at Ascent Business Solutions recognized as the top Zoho Premium Solutions Partner in the United Kingdom.

Ascent Business Solutions offer support for smaller technical fixes and projects for larger developments, such as migrating to a ZohoCRM.  A team rather than a one-man-band is always available to ensure seamless progress and address any concerns. You'll find our competitive support rates with flexible, no-expiration bundles at https://ascentbusiness.co.uk/zoho-services/uk-zoho-support.  For larger projects, talk to our experts and receive dedicated support from our hands-on project consultants at https://ascentbusiness.co.uk/zoho-services/zoho-crm-implementation.

The team I manage specializes in coding API integrations between Zoho and third-party finance/commerce suites such as Xero, Shopify, WooCommerce, and eBay; to name but a few.  Our passion lies in creating innovative solutions where others have fallen short as well as working with new businesses, new sectors, and new ideas.  Our success is measured by the growth and ROI we deliver for clients, such as transforming a garden shed hobby into a 250k monthly turnover operation or generating a +60% return in just three days after launch through online payments and a streamlined e-commerce solution, replacing a paper-based system.

If you're looking for a partner who can help you drive growth and success, we'd love to work with you.  You can reach out to us on 0121 392 8140 (UK) or info@ascentbusiness.co.uk.  You can also visit our website at https://ascentbusiness.co.uk.
Zoho CRM: Remove Duplicate Product Records

Zoho CRM: Remove Duplicate Product Records

What?
An article on removing duplicate products within ZohoCRM.

Why?
Product duplicates are a common issue during development, especially when we have products imported via a feed or API or simply by staff and their spreadsheets. There is a deduplication feature for leads and contacts but not for products.

How?
There may be a better way of doing this. What I'm trying to have is a generic function that will work on any CRM and possibly be quick to adapt to a module other than products.

This function will also try to delete the duplicate but if the duplicate is the one used in a transactional module record (eg. quotes, sales orders, invoices, purchase orders), then it will delete the older one (referred to as keep ID).

Another point is that this is using COQL to sort the records and tries to keep the first version of the record (sorted by ID ascending) and will attempt to delete the other records which match on product name.

I would recommend commenting out the delete commands and leaving the info commands to preview what records it will delete. I also recommend doing a data backup beforehand.

Adapt the below as needed:
copyraw
string standalone.fn_Products_RemoveDuplicates()
{
/* *******************************************************************************
	Function:       string standalone.fn_Products_RemoveDuplicates()
	Label:          fn - Products - Remove Duplicates
	Trigger:        Standalone
	Purpose:		Used to remove duplicate products from a ZohoCRM instance
	Inputs:         -
	Outputs:        -

	Date Created:   2025-03-07 (Joel Lipman)
					- Initial release
	Date Modified:	???
					- ???
	More Info:		-

	******************************************************************************* */
	//
	v_CountUnique = 0;
	v_CountTotal = 0;
	v_CountDeleted = 0;
	//
	// pagination
	v_PerPage = 500;
	//
	// loop through pages to fetch as many as possible
	for each  v_Page in {1,2}
	{
		info "Page #" + v_Page;
		v_PageFactor = v_Page - 1;
		v_Offset = v_PageFactor * v_PerPage;
		//
		// get unique product names  (v2 up to 200, v6 up to 10k, v7 up to 100k)
		m_Params = {"select_query":"select Product_Name, COUNT(id) 'Frequency' from Products where Product_Name is not null group by Product_Name limit " + v_PerPage + " offset " + v_Offset};
		r_CoqlUniqueProducts = invokeurl
		[
			url :"https://www.zohoapis.eu/crm/v7/coql"
			type :POST
			parameters:m_Params.toString()
			connection:"zcrm"
		];
		l_UniqueProducts = ifnull(r_CoqlUniqueProducts.get("data"),List());
		for each  m_Product in l_UniqueProducts
		{
			l_DebugNotes = List();
			v_CountUnique = v_CountUnique + 1;
			v_ProductName = m_Product.get("Product_Name");
			v_Frequency = m_Product.get("Frequency");
			v_Debug = v_CountTotal + ": " + v_ProductName + ": " + v_Frequency + ":- ";
			//
			// only use up more api calls if occurs more than once
			if(v_Frequency > 1)
			{
				//
				// replace apostrophes with double apostrophe for coql
				v_SearchName = v_ProductName.replaceAll("'","''",true);
				//
				v_KeepID = 0;
				//
				// get other products with the same name (set in order of oldest first and keep only first one, delete the others)
				m_SubParams = {"select_query":"select id, Product_Name from Products where Product_Name = '" + v_SearchName + "' order by id limit " + v_PerPage};
				r_CoqlMatchedProducts = invokeurl
				[
					url :"https://www.zohoapis.eu/crm/v7/coql"
					type :POST
					parameters:m_SubParams.toString()
					connection:"zcrm"
				];
				l_MatchedProducts = ifnull(r_CoqlMatchedProducts.get("data"),List());
				for each  m_Result in l_MatchedProducts
				{
					// ensure we are looping through results and not a search error
					if(!isNull(m_Result.get("id")))
					{
						v_CountTotal = v_CountTotal + 1;
						// 
						// keep the first one in results
						if(v_KeepID == 0)
						{
							v_KeepID = m_Result.get("id").toLong();
							l_DebugNotes.add("Will keep ID: " + m_Result.get("id"));
						}
						else
						{
							l_DebugNotes.add("Will delete ID: " + m_Result.get("id"));
							m_Delete = Map();
							m_Delete.put("module","Products");
							m_Delete.put("id",m_Result.get("id"));
							r_Delete = zoho.crm.invokeConnector("crm.delete",m_Delete);
							//
							// if error that product is already linked in a transactional module record.
							if(r_Delete.containsIgnoreCase("cannot be deleted") && r_Delete.containsIgnoreCase("involved in inventory record"))
							{
								l_DebugNotes.add("CANCELLED DELETION. Deleting Keep ID: " + v_KeepID);
								m_Delete = Map();
								m_Delete.put("module","Products");
								m_Delete.put("id",v_KeepID.toString());
								r_Delete = zoho.crm.invokeConnector("crm.delete",m_Delete);
								info r_Delete;
							}
							else
							{
								v_CountDeleted = v_CountDeleted + 1;
							}
						}
					}
				}
			}
			else
			{
				l_DebugNotes.add("No Action.");
			}
			info v_Debug + l_DebugNotes.toString(". ");
		}
		if(l_UniqueProducts.size() == 0)
		{
			break;
		}
	}
	return "Deleted " + v_CountDeleted + " of " + v_CountTotal;
}
  1.  string standalone.fn_Products_RemoveDuplicates() 
  2.  { 
  3.  /* ******************************************************************************* 
  4.      Function:       string standalone.fn_Products_RemoveDuplicates() 
  5.      Label:          fn - Products - Remove Duplicates 
  6.      Trigger:        Standalone 
  7.      Purpose:        Used to remove duplicate products from a ZohoCRM instance 
  8.      Inputs:         - 
  9.      Outputs:        - 
  10.   
  11.      Date Created:   2025-03-07 (Joel Lipman) 
  12.                      - Initial release 
  13.      Date Modified:    ??? 
  14.                      - ??? 
  15.      More Info:        - 
  16.   
  17.      ******************************************************************************* */ 
  18.      // 
  19.      v_CountUnique = 0
  20.      v_CountTotal = 0
  21.      v_CountDeleted = 0
  22.      // 
  23.      // pagination 
  24.      v_PerPage = 500
  25.      // 
  26.      // loop through pages to fetch as many as possible 
  27.      for each  v_Page in {1,2} 
  28.      { 
  29.          info "Page #" + v_Page; 
  30.          v_PageFactor = v_Page - 1
  31.          v_Offset = v_PageFactor * v_PerPage; 
  32.          // 
  33.          // get unique product names  (v2 up to 200, v6 up to 10k, v7 up to 100k) 
  34.          m_Params = {"select_query":"select Product_Name, COUNT(id) 'Frequency' from Products where Product_Name is not null group by Product_Name limit " + v_PerPage + " offset " + v_Offset}
  35.          r_CoqlUniqueProducts = invokeUrl 
  36.          [ 
  37.              url :"https://www.zohoapis.eu/crm/v7/coql" 
  38.              type :POST 
  39.              parameters:m_Params.toString() 
  40.              connection:"zcrm" 
  41.          ]
  42.          l_UniqueProducts = ifnull(r_CoqlUniqueProducts.get("data"),List())
  43.          for each  m_Product in l_UniqueProducts 
  44.          { 
  45.              l_DebugNotes = List()
  46.              v_CountUnique = v_CountUnique + 1
  47.              v_ProductName = m_Product.get("Product_Name")
  48.              v_Frequency = m_Product.get("Frequency")
  49.              v_Debug = v_CountTotal + ": " + v_ProductName + ": " + v_Frequency + ":- "
  50.              // 
  51.              // only use up more api calls if occurs more than once 
  52.              if(v_Frequency > 1) 
  53.              { 
  54.                  // 
  55.                  // replace apostrophes with double apostrophe for coql 
  56.                  v_SearchName = v_ProductName.replaceAll("'","''",true)
  57.                  // 
  58.                  v_KeepID = 0
  59.                  // 
  60.                  // get other products with the same name (set in order of oldest first and keep only first one, delete the others) 
  61.                  m_SubParams = {"select_query":"select id, Product_Name from Products where Product_Name = '" + v_SearchName + "' order by id limit " + v_PerPage}
  62.                  r_CoqlMatchedProducts = invokeUrl 
  63.                  [ 
  64.                      url :"https://www.zohoapis.eu/crm/v7/coql" 
  65.                      type :POST 
  66.                      parameters:m_SubParams.toString() 
  67.                      connection:"zcrm" 
  68.                  ]
  69.                  l_MatchedProducts = ifnull(r_CoqlMatchedProducts.get("data"),List())
  70.                  for each  m_Result in l_MatchedProducts 
  71.                  { 
  72.                      // ensure we are looping through results and not a search error 
  73.                      if(!isNull(m_Result.get("id"))) 
  74.                      { 
  75.                          v_CountTotal = v_CountTotal + 1
  76.                          // 
  77.                          // keep the first one in results 
  78.                          if(v_KeepID == 0) 
  79.                          { 
  80.                              v_KeepID = m_Result.get("id").toLong()
  81.                              l_DebugNotes.add("Will keep ID: " + m_Result.get("id"))
  82.                          } 
  83.                          else 
  84.                          { 
  85.                              l_DebugNotes.add("Will delete ID: " + m_Result.get("id"))
  86.                              m_Delete = Map()
  87.                              m_Delete.put("module","Products")
  88.                              m_Delete.put("id",m_Result.get("id"))
  89.                              r_Delete = zoho.crm.invokeConnector("crm.delete",m_Delete)
  90.                              // 
  91.                              // if error that product is already linked in a transactional module record. 
  92.                              if(r_Delete.containsIgnoreCase("cannot be deleted") && r_Delete.containsIgnoreCase("involved in inventory record")) 
  93.                              { 
  94.                                  l_DebugNotes.add("CANCELLED DELETION. Deleting Keep ID: " + v_KeepID)
  95.                                  m_Delete = Map()
  96.                                  m_Delete.put("module","Products")
  97.                                  m_Delete.put("id",v_KeepID.toString())
  98.                                  r_Delete = zoho.crm.invokeConnector("crm.delete",m_Delete)
  99.                                  info r_Delete; 
  100.                              } 
  101.                              else 
  102.                              { 
  103.                                  v_CountDeleted = v_CountDeleted + 1
  104.                              } 
  105.                          } 
  106.                      } 
  107.                  } 
  108.              } 
  109.              else 
  110.              { 
  111.                  l_DebugNotes.add("No Action.")
  112.              } 
  113.              info v_Debug + l_DebugNotes.toString(". ")
  114.          } 
  115.          if(l_UniqueProducts.size() == 0) 
  116.          { 
  117.              break
  118.          } 
  119.      } 
  120.      return "Deleted " + v_CountDeleted + " of " + v_CountTotal; 
  121.  } 


Category: Zoho :: Article: 899

Add comment

Your rating:

Submit

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

Please publish modules in offcanvas position.