cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Access catalogVersionService from Groovy class

former_member633554
Active Participant
0 Likes
1,591

Hi all , i'm trying to access the catalogversionservice through groovy, yet this throws up errors. It works fine if it's not in a class.

The error i get is Script execution has failed [reason: java.lang.NullPointerException: Cannot invoke method getCatalogVersion() on null object]

 import de.hybris.platform.catalog.model.CatalogVersionModel;
 import de.hybris.platform.catalog.model.CatalogModel;
 import de.hybris.platform.core.model.product.ProductModel;
 import de.hybris.platform.product.ProductService;
 import de.hybris.platform.catalog.CatalogVersionService;
 import de.hybris.platform.catalog.model.classification.ClassAttributeAssignmentModel;
 import de.hybris.platform.catalog.model.classification.ClassificationClassModel;
 import de.hybris.platform.classification.features.*;
 
 public class MainClass
 {
   
  def outputData()
   {
     CatalogVersionModel catalogVersion=catalogVersionService.getCatalogVersion("Maincat", "Staged");
  ProductModel product1 = productService.getProductForCode(catalogVersion,"PROD_1");
   
   }
 }
 
 new MainClass().outputData()
 

Accepted Solutions (1)

Accepted Solutions (1)

andyfletcher
Active Contributor
0 Likes

catalogVersionService is not in scope. It belongs to the implicit script class and is therefore not accessible from an instance of MainClass

You could do something like this to pass the catalogVersionService in when you construct the instance of MainClass

 public class MainClass {
   def catalogVersionService
   def productService
    
   def outputData() {
     def catalogVersion = catalogVersionService.getCatalogVersion('Maincat', 'Staged')
     def product1 = productService.getProductForCode(catalogVersion, 'PROD_1')
     println product1.name
   }
 }
  
 new MainClass(catalogVersionService: catalogVersionService, productService: productService).outputData()

Answers (3)

Answers (3)

former_member633554
Active Participant
0 Likes

This also works from within a method in a class.

  CatalogVersionService catalogVersionService = (CatalogVersionService) Registry.getApplicationContext().getBean("catalogVersionService");
   CatalogVersionModel catVersion = catalogVersionService.getCatalogVersion("Maincat", "Staged");

Former Member
0 Likes

Hi , While using groovy , you can access the spring beans directly by their bean id. In your case , rather using defaultCatalogVersionService would probably do the trick.

former_member638520
Contributor
0 Likes

Hi,

Have you checked this document? https://help.hybris.com/6.4.0/hcd/8bec04a386691014938a9996a977d07f.html

Have you tried to get catalogVersionService by:

 catalogVersionService = spring.getBean "catalogVersionService"

?

Regards

Lukasz