Solr

There should not be any surprise if any Hybris developer is asked to write Groovy script

Groovy is a scripting language based on Java.

It is used in Hybris world mainly for debugging and troubleshooting purpose

Using Groovy , we can run the scripts inside Hybris without modifying the source code.

It is very powerful for debugging purpose especially remote system

Example :
If we want to get the list of products which does not have any category associated with it
We can get it using groovy as below

  1. import de.hybris.platform.catalog.model.CatalogVersionModel
  2. import de.hybris.platform.core.model.product.ProductModel
  3.  
  4. findProductsWithoutCategories()
  5.  
  6. defaultProductService = spring.getBean "defaultProductService"
  7. catalogVersionService = spring.getBean "catalogVersionService"
  8.  
  9. def findProductsWithoutCategories (){
  10.     CatalogVersionModel categoryVersionModel = catalogVersionService.getCatalogVersion("apparelProductCatalog", "Staged")
  11.     List<ProductModel> productModelList = defaultProductService.getAllProductsForCatalogVersion(categoryVersionModel)
  12.  
  13.     if(!productModelList.isEmpty()) {
  14.         for(ProductModel product: productModelList) {
  15.             if(product.getSupercategories().isEmpty()){
  16.                 println " Product “+ product.getCode() +” does not have any categories associated with it :"
  17.             }
  18.         }
  19.     }
  20. }
import de.hybris.platform.catalog.model.CatalogVersionModel
import de.hybris.platform.core.model.product.ProductModel

findProductsWithoutCategories()

defaultProductService = spring.getBean "defaultProductService"
catalogVersionService = spring.getBean "catalogVersionService"

def findProductsWithoutCategories (){
	CatalogVersionModel categoryVersionModel = catalogVersionService.getCatalogVersion("apparelProductCatalog", "Staged")
	List<ProductModel> productModelList = defaultProductService.getAllProductsForCatalogVersion(categoryVersionModel)

	if(!productModelList.isEmpty()) {
		for(ProductModel product: productModelList) {
			if(product.getSupercategories().isEmpty()){
				println " Product “+ product.getCode() +” does not have any categories associated with it :"
			}
		}
	}
}


To run the Groovy script, navigate to http://localhost:9001/hac/console/scripting

Make sure Groovy is selected in the dropdown

paste the Groovy script under Edit statement, and click on Execute.

Result : display results returned by the script.
Output : display outputs raised by the script.
Stack trace : display error thrown by the script.

Share this article on

Comment (9)

    1. Karibasappa G C (KB)
      Karibasappa G C (KB)

      First thing, static variables will never be a part of method, it will be a part of class always.

      So only we cant declare any static variables inside a method but we can access them inside a method.

      All static variables will be stored in separate segment inside Heap area only.

      1. The blog is very helpful. Thanks a lot for sharing! Also the “Inheritance in interface” page is not opening. Can you please fix that?

Leave a Comment

  • You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" extra="">

    • Please answer this simple challenge to post your valuable comment *