How to create a Cronjob using Groovy script in SAP Hybris


You can skip scenario and root cause section below, if you only interested in the groovy script cronjob steps

Scenario

When we create the ProductReference inside the Product(for stage catalog) using Impex/API. The owning product is not getting sync when we do a catalog sync.

Root Cause

As per wiki
Modification time issues:
  • Keep in mind that changing a part-of item does not mark its owning item modified automatically. To allow synchronization to schedule the owning item correctly you have to mark it modified manually, preferably inside the part-of item's business code.
So here, we can write After Save Event or groovy script to update its owning item(Product) modification time.
Now let's see, how can we handle this using groovy script in SAP Hybris.

Solution

To configure the groovy script as a cronjob, you have to create an instance of
Script - the item type where the script content is going to store.
ScriptingJob - a new ServicelayerJob item, which contains additionally the scriptURI
CronJob - This is where the "scripted" cronjob logic is executed

Now let's go step by step
1. Create a Script
From HMC/BackOffice, Right click on Script to create a new script.
Script code: UpdateProductModifiedtimeGroovyScript
Script engine type: GROOVY
Content:
import java.util.Calendar;
import de.hybris.platform.servicelayer.search.FlexibleSearchQuery;

Date now = Calendar.getInstance().getTime();

println "Update Product modifiedTime for ProductReference";
flexibleSearchService.search(/ 
   select DISTINCT {p.pk} from
   {Product as p JOIN  ProductReference AS pr ON {p.pk} = {pr.source}}
  where {pr.modifiedTime} > {p.modifiedTime}
 /).result.each {
   count++;
   println it.getCode();
   it.setModifiedtime(now);
   modelService.save(it); 
}
2. Create the Scripting Job
Again from HMC/Backoffice, find ScriptinJobs and create the new instance of it. Here you have to define Code and ScriptURI like
Code: UpdateProductModifiedtimeGroovyJob
ScriptURI: model://UpdateProductModifiedtimeGroovyScript
You can access this job in the next step to create the CronJob
3. Create a CronJob
From HMC/BackOffice, create an instance of cronJob. Select above-created job(UpdateProductModifiedtimeGroovyJob) in
Job definition dropdown and save the changes. Now you good to schedule/run this cronJob.


Author Image

Ankitkumar Patel

Sr. SAP Hybris consultant, having 6+ years experience in Hybris, Java, J2EE. Extensive hands-on experience in SAP Hybris development, third-party integrations with Hybris, project architecture and design... Read more

Comments

Popular posts from this blog

Hybris flexible search query examples

How to remove or update all data records in Hybris?

Hybris backoffice customization