A note from 2026: This article was published in 2018. SAP Hybris is now SAP Commerce Cloud, and cloud deployments have stricter access controls; direct filesystem access, HAC Groovy execution, Jalo APIs, and some internal `de.hybris.*` utilities may be restricted or unsupported depending on your version and environment.

Previous parts:

Executing an SQL Query. For hybris 6.0+

In the latest versions of SAP Hybris, the classes from HAC are no longer available in Groovy. The code I published before won’t work anymore.

sql = "select pk, p_isocode from languages";
result = jdbcTemplate.queryForList(sql);
result[0].keySet().forEach { print it + "\t"; }
println "";
result.forEach
{
    it.values().forEach { print it + "\t"; }
    println "";
}

The result can be copied to Excel for further processing. Paste it as text, and don’t forget to change the cell types to Text to avoid automatic cell value type detection in Excel.

SQL query results copied from SAP Hybris HAC to a table

Accessing the file system and fetching a text file via HAC

For example, the following one-liner shows the contents of localextensions.xml.

"cat ../../../../config/localextensions.xml".execute().text

Contents of localextensions.xml displayed in SAP Hybris HAC

Invalidating a cache for a particular item

cv = catalogVersionService.getCatalogVersion('mycatalog', 'Online')
p = productService.getProductForCode(cv, '123456789')

de.hybris.platform.util.Utilities.invalidateCache(p.pk)

modelService.refresh(p)
"" + p.pk + ":" + p.name

Executing the multi-column flexibleSearch

import de.hybris.platform.jalo.JaloSession;

query = "select {isocode}, {name} from {Language} ";
jaloSession = JaloSession.getCurrentSession();
List result = jaloSession.getFlexibleSearch().search(
    query,
    Collections.EMPTY_MAP,
    Arrays.asList(
        //types of arguments in Select
        String.class,
        String.class
        //String.class, String.class
        //
    ),
    true,
    true,
    0, -1
).getResult();

result.forEach {
    it.forEach { print it + "\t"; }
    println "";
}

The result can be copied to Excel for further processing. Paste it as text, and don’t forget to change the cell types to Text to avoid automatic cell value type detection in Excel.

Executing a long/resource-intensive Groovy script

There is a common problem: some scripts need more time than Tomcat/hybris allows. After a couple of minutes, the system kills the script. To overcome this problem, you need to create a cronjob with this script and launch it. However, remember that if your script is resource-intensive, you may face a lack of resources, and it is not fixable: you won’t be able to stop the script after it is launched.

There are two options. The first one is creating a thread, and the second one is creating a cronjob.

Thread

import org.apache.logging.log4j.LogManager;

def log(String s) {
    println(s)
    LogManager.getLogger().info(s)
}

def thread = Thread.start {
    sleep(2000)
    log "new thread - this line will be in the hybris log only after 2 sec"
    sleep(20000)
    log "new thread 20sec finished - this line will be in the hybris log only after 20 sec"
    sleep(20000)
    log "new thread last 20sec finished - this line will be in the hybris log only after 40 sec"
}

log "immediate message in the groovy console and hybris log"

Cronjob

First, you need to create a Groovy script and save it in HAC as “test1”. This script name is specified in the script below. Of course, you can use any name instead of “test1”; just replace it with your script name in the fourth line of the script below.

As an example of a long-running script, let’s take one that shows the files from the hybris media folder.

def p = ['/bin/bash', '-c', 'ls -lR ../../../../data/media/sys_master/* > /tmp/media-folder-size.tmp'].execute()
p.waitFor()
println p.text
import de.hybris.platform.servicelayer.internal.model.ScriptingJobModel;
import de.hybris.platform.cronjob.model.CronJobModel;

scriptName = "test1"
CRONJOBNAME = "myHacScriptCronJob1";
MYHACSCRIPTJOBNAME = "myHacScriptJob1"

def scriptingJob = null;
def myHacScriptDynamicCronJob = null;

scriptingJobResults = flexibleSearchService.search("select {pk} from {ScriptingJob} where {code} = '" + MYHACSCRIPTJOBNAME + "'").getResult();

if (scriptingJobResults.size() > 0) {
    scriptingJob = scriptingJobResults.get(0)
} else {
    scriptingJob = modelService.create(ScriptingJobModel.class);
    scriptingJob.setCode(MYHACSCRIPTJOBNAME);
    scriptingJob.setScriptURI("model://" + scriptName);
    modelService.save(scriptingJob);
}

try {
    myHacScriptDynamicCronJob = cronJobService.getCronJob(CRONJOBNAME);
} catch (de.hybris.platform.servicelayer.exceptions.UnknownIdentifierException e) {
    myHacScriptDynamicCronJob = modelService.create(CronJobModel.class);
    myHacScriptDynamicCronJob.setCode(CRONJOBNAME);
    myHacScriptDynamicCronJob.setJob(scriptingJob);
    myHacScriptDynamicCronJob.setActive(true);
    myHacScriptDynamicCronJob.setSingleExecutable(true);
    myHacScriptDynamicCronJob.setLogToFile(true);
    modelService.save(myHacScriptDynamicCronJob);
}

dynamicCJ = cronJobService.getCronJob(CRONJOBNAME)
cronJobService.performCronJob(dynamicCJ, true)

After the cronjob is over, open the temporary file from /tmp/media-folder-size.tmp to see the results:

"cat /tmp/media-folder-size.tmp".execute()

SAP Hybris HAC output showing media folder size temporary file results

For this particular case, the large size of the file can be an issue. This file can be preprocessed in Groovy before being delivered. For example, all non-relevant stuff, such as file flags, a user ID, and a group ID, can be removed.