Delete Hybris Products having duplicate name from HAC

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

 


Reference : https://www.helphybris.com/2018/03/delete-hybris-products-having-duplicate.html

Comments

Popular Posts