Groovy scripts overview

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.

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on