Delete Hybris Products having duplicate name from HAC

How to delete duplicate product using the flexible search in Hybris?

Open HAC > console > scripting languages
Now you can run the below groovy script

1. Groovy script to get the list of pks to be removed

import de.hybris.platform.servicelayer.search.FlexibleSearchQuery;
flexibleSearchService = spring.getBean("flexibleSearchService")
modelService = spring.getBean("modelService")
def findProductWithSameName() {
    query = """ select MIN({p.pk}) as pks
 from {Product! as p

 JOIN CatalogVersion as CV on {p.catalogversion}={CV:PK} and {CV:version} = 'Online' 
 JOIN Catalog as C on {CV:catalog}={C:PK} and {C:id}='myProductCatalog'

 } 

 group by {p:name}

 having 
   count(*) > 1""";
    flexibleSearchService.search(query).result;
}
findProductWithSameName().each {

   println it.code;

   //modelService.remove(it);
}


2. Test Remove script

import de.hybris.platform.servicelayer.search.FlexibleSearchQuery;
flexibleSearchService = spring.getBean("flexibleSearchService")
modelService = spring.getBean("modelService")
def findProductWithSameName() {
    query = """ select MIN({p.pk}) as pks
 from {Product! as p

 JOIN CatalogVersion as CV on {p.catalogversion}={CV:PK} and {CV:version} = 'Online' 
 JOIN Catalog as C on {CV:catalog}={C:PK} and {C:id}='myProductCatalog'

 } 

 group by {p:name}

 having 
   count(*) > 1""";
    flexibleSearchService.search(query).result;
}
findProductWithSameName().each {

   //println it.code;

   modelService.remove(it);
}

This will run in ROLLBACK mode, so your data is still not deleted. You should not get any error till now.


3. Run the Remove script

Enable the commit mode by clicking ROLLBACK button. Now run remove query mentioned in step 2 to delete all duplicate products from the stage version. Repeat all step for Onlineversion as well
Author Image

Ankitkumar Patel

Sr. SAP Hybris consultant, having 15+ years experience in SAP Commerce Cloud (Hybris), SAP Spartacus. Extensive experience in SAP Hybris development, third-party integrations, project architecture and design... Read more

Comments

Popular posts from this blog

Hybris flexible search query examples

How to remove or update all data records in Hybris?

How to Install temporary Hybris license?