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; }
- 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;
- }
Category: Zoho :: Article: 899
Add comment