Use Groovy script in SAP Hybris

Hybris featured image

1. Overview

Groovy is a script programming language based on Java, it is used in the Hybris world to create and run some very quick tasks, like cleaning and testing.

In this Article, I will show how to create and how to run a Groovy script in Hybris.

2. Implementation

The following Groovy script publish a HelloWorldEvent, using EventService.

// import here
import de.hybris.platform.servicelayer.event.EventService
import com.stackextend.training.core.event.HelloWorldEvent

// retrieve beans from Spring context
EventService eventService = spring.getBean("eventService")

// do business logic
HelloWorldEvent event = new HelloWorldEvent("Mouad EL Fakir");

eventService.publishEvent(event);

// return result
return "OK"

Refer to this article for more details about creating the HelloWorldEvent.

To run the Groovy script, navigate to http://localhost:9001/hac/console/scripting,  past the Groovy script under Edit statement, and press Execute.

Run Groovy script in HAC Hybris

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

Change ROLLBACK to COMMIT if you want to persist results in the database.

 

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of