Use Scripting Job in SAP Hybris
1. Overview
The scripting job is a very particular job in Hybris, it gives you the possibility to run scripts of type Groovy, BeanShell or Javascript as a cronJob.
- Script : is a statement in Groovy, BeanShell or Javascript, in Hybris it’s an instance of the
ScriptModel
class. - Scripting Job : is an instance of the
ScriptingJobModel
, It takes as a parameter the URI to the script, the URI could use http, https, ftp, file, classpath or model. - Cron job : the cron job is an instance of the
CronJobModel
, it takes in parameter the Scripting Job.
In this article, I will show you how to create a scripting job in Hybris.
2. Scripting Job in action
2.1. Script
First, let’s create our script Groovy, via the impex.
INSERT_UPDATE Script;code[unique=true] ;scriptType(code) ;content
;helloWorldScript ;GROOVY ;println "Hello World!"
2.2. Scripting Job
A scripting job is an instance of the ScriptingJobModel
, the scripting job will be referring to our script helloWorldScript using URI of type model.
INSERT_UPDATE ScriptingJob ;code[unique=true] ;scriptURI
;helloWorldScriptJob ;model://helloWorldScript
You can use any URI protocol (http, ftp, file, classpath…) you want for the scriptURI.
2.3. Cron Job
Last and not least, create an instance of the CronJobModel
and attach to it the scripting job helloWorldScriptJob.
INSERT_UPDATE CronJob ;code[unique=true] ;job(code) ;sessionLanguage(isocode) ;sessionUser(uid)
;helloWorldScriptCronJob ;helloWorldScriptJob ;en ;admin
Start the cron job helloWorldScriptCronJob manually from the HMc/Backoffice, or attach a trigger to it.
If everything goes well, you should have a log similar to this :
18.09.25 00:22:39:247 INFO ### Starting executing script : model://helloWorldScript ###
18.09.25 00:22:39:247 INFO Hello World!
18.09.25 00:22:39:247 INFO ### Finished executing script, returned script result = null ###
Software Craftsmanship, Stackextend author and Full Stack developer with 6+ years of experience in Java/Kotlin, Java EE, Angular and Hybris…
I’m Passionate about Microservice architectures, Hexagonal architecture, Event Driven architecture, Event Sourcing and Domain Driven design (DDD)…
Huge fan of Clean Code school, SOLID, GRASP principles, Design Patterns, TDD and BDD.
brief, simple and useful, thank you (y)
Thanks Ayoub !
simple and perfect explanation
Thank you Teja!
perfect ! thanks!
what is the use of scripting?
Refer to Lahcen’s comment 🙂
Very interesting, will keep in mind … i was asking myself about some benefits, and I think there should be a lot: – Runtime extensibility: you can schedule technical / functional task while your Hybris is up an running (to be done in a moderate way, otherwise will end up with scattered codebase ) – Open the door to other languages (I know you have already Scripting console (HAC), but here you can do it in a periodic-basis) – It could be a good candidate to add orthogonal (ie not linked to a specific business case) functionality; exp: Schedule a… Read more »
Thanks Lahcen for getting in touch, your comment somehow answers @Vinay’s question 🙂
However, for the limitations, I think (as far as I know) with a scripting language like Groovy we should be able to do everything native Java can do, including accessing runtime resources (spring context, database, resources and bundles…)
Thanks
Hello, in a scenario where i want to enter a script as below, how to handle ; and line separators ??? // Create a temporary file for the demo file = File.createTempFile(“stuff”,”.tmp”) file.deleteOnExit() pw = new PrintWriter(file) pw.println ‘This is line 1’ pw.println ‘This is line 2’ pw.close() // Create the email email = de.hybris.platform.util.mail.MailUtils.getPreConfiguredEmail() email.addTo(‘[email protected]’) email.subject = ‘Important stuff attached’ email.msg = ‘Here is your attachment’ // Create an attachment that is our temporary file attachment = new org.apache.commons.mail.EmailAttachment(); attachment.path = file.absolutePath attachment.disposition = org.apache.commons.mail.EmailAttachment.ATTACHMENT attachment.description = ‘Stuff’ attachment.name = ‘stuff.txt’ // Attach the attachment email.attach(attachment) // Send the… Read more »
thank you!