Use Scripting Job in SAP Hybris

Hybris Logo

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.

Scripting job in Hybris

  • 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.

Start scripting Conjob from hmc in Hybris

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 ###

 

0 0 votes
Article Rating
Subscribe
Notify of
guest

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

11 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Ayoub
Ayoub
2 years ago

brief, simple and useful, thank you (y)

Teja
Teja
2 years ago
Reply to  Ayoub

simple and perfect explanation

yan
yan
2 years ago
Reply to  Mouad EL Fakir

perfect ! thanks!

vinay
vinay
2 years ago

what is the use of scripting?

Lahcen Zbair
Lahcen Zbair
2 years ago

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 »

vinay
vinay
2 years ago
Reply to  Lahcen Zbair

Thanks

Rohit
Rohit
2 years ago

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(‘r.aliev@gmail.com’) 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 »