« Back

5 tips to improve usage of the Liferay script console

General Blogs May 12, 2013 By Sébastien Le Marchand

// La version française de cet article est là : 5 astuces pour améliorer l’usage de la console de script de Liferay.

// If you use a RSS reader to read this post, please switch to the HTML page to correctly view the code snippets.

Since the release 6.0, Liferay provide a script console integrated into the Control Panel. Very powerful, this tool becomes fast essential for any portal administrator or developer (if you do not still know the script console, have a look on this article by Jeffrey Handa).

During this post, I am going to present you 5 simple tips to take better advantage of the script console.

The following implementation samples are based on the Groovy language, but most of them are applicable with other languages supported by the portal script console.
 
 

Tip #1: Use the HTML in your outputs

You need to know that the script console default output is rendered as HTML content. Thus, think to embed HTML markup in your outputs to enjoy rich formatting.
 
Script example: 
 
Output:
 
 

Tip #2: Show up exceptions on user interface

When any exception arises during script execution, the error message is always the same and give no detail about the error. So, you need to access server logs to make a diagnostic.
 
 
To show up exception details on the user interface, envelop your whole code with a try/catch block and print the stack trace on the console output (from the catch clause). There is nevertheless a limitation: it doesn’t work for script syntax errors.
 
Script example:
Output: 
 
 

Tip #3: Exploit the predefined variables 

A script executed from the Control Panel can access the following predefined variables :
  • out (java.io.PrintWriter)
  • actionRequest (javax.portlet.ActionRequest)
  • actionResponse (javax.portlet.ActionResponse)
  • portletConfig (javax.portlet.PortletConfig)
  • portletContext (javax.portlet.PortletContext)
  • preferences (javax.portlet.PortletPreferences)
  • userInfo (java.util.Map<String, String>)
When you write your scripts, be aware of possibilities offered by these variables, particularly the actionRequest variable, useful for a lot of method calls.
 
Script example:
Output:
 
 

Tip #4: Implement a preview mode

The script console not allowing execution rollback, it’s very convenient to set up a preview mode for side-effect scripts (i.e. performing database updates).
 
Ce mode "preview" consiste à utiliser un flag qui détermine si les les opérations à effets de bord sont exécutées ou non, alors que les données parcourues sont systématiquement tracées. Ceci permet donc de se faire une idée des données impactées par un script avant de faire effectivement les mise à jour en base de données.
 
This "preview mode" consists in using a flag which determines if the operations with side effects are executed or not, while the visited data are systematically printed. This thus allows to have an outline of the data impacted by the script, before effectively doing updates to database.
 
Script example :   

 

Tip #5: Plan a file output for "long-running" scripts

When the execution of a script exceeds a certain duration, the console doesn’t respond any more and returns an error, whereas the script can continue its execution and potentially bring to a successful conclusion... But impossible to know the status without the corresponding output!
 
To by-pass this limitation, you can set up a file output besides the console output, like in the following example: 
Once the initial script was executed, it is possible to read the generated  file without direct access to the file system, by using a second script:
Note that method based on a dedicated output file is better than using a classic logger because it’s easier to get back the script output data (which would be more difficult to isolate among all  portal logs).
 
And you, do you have some tips about script console that you can share with the community ? Let us know in the comments !
 
Further reading:
Threaded Replies Author Date
Sébastien, very very nice post! A+ on... James Falkner May 13, 2013 6:28 AM
Very nice article :) Kailash Yadav May 13, 2013 9:56 AM
[...] // In reply to 5 tips to improve usage of... Anonymous May 13, 2013 11:59 AM
Tip #6: Schedule your script to run as a CRON... Kan Zhang May 13, 2013 12:00 PM
Very Interesting :) Jay Patel June 20, 2013 12:31 PM
Great article! Thanks Sébastien, I was just... Enrique Valdes Lacasa October 3, 2014 12:44 PM
[...] Running Scripts from the Control Panel 5... Anonymous November 12, 2014 1:32 PM
Really nice! Jörg Halitschke October 15, 2015 4:10 AM
Very nice explanation, I am troubling to access... Kashif Ayyaz Haider December 11, 2015 3:38 AM
Hi Kashif, If your'e using a Service from a... Jeffrey Handa December 21, 2015 5:09 PM

Sébastien, very very nice post! A+ on formatting, use of Gist, translation, and of course dynamic scripting for the purposes of debugging and prototyping is really really useful for all devs.. Thanks, and keep 'em coming!
Posted on 5/13/13 6:28 AM.
Posted on 5/13/13 9:56 AM.
[...] // In reply to 5 tips to improve usage of the Liferay script console - by Sébastien Le Marchand [...] Read More
Posted on 5/13/13 11:59 AM.
Tip #6: Schedule your script to run as a CRON job
http://www.liferay.com/web/kzhang/blog/-/blogs/re-5-tips-to-improve-usage-of-­the-liferay-script-console

I post it as a sepreate blog entry because I cannot use GIST or format in comment area...
Posted on 5/13/13 12:00 PM.
Posted on 6/20/13 12:31 PM.
Great article! Thanks Sébastien, I was just asking myself if I could use the actionRequest variable in the scripts...tip number 3 was what I needed.
Posted on 10/3/14 12:44 PM.
[...] Running Scripts from the Control Panel 5 tips to improve usage of the Liferay script console [...] Read More
Posted on 11/12/14 1:32 PM.
Posted on 10/15/15 4:10 AM.
Very nice explanation, I am troubling to access custom service using your tips but no success.

I have my own table generated via service builder say Book
when i call

books = BookLocalServiceUtil.getBooks(QueryUtil.ALL_POS, QueryUtil.ALL_POS)

it says
No such property: BookLocalServiceUtil for class: Script5

i have imported service package of my portlet as well here
Posted on 12/11/15 3:38 AM.
Hi Kashif,

If your'e using a Service from a plugin you'll need to use the PortletBeanLocatorUtil.locate() method.

https://docs.liferay.com/portal/6.2/javadocs/com/liferay/portal/kernel/bean/Port­letBeanLocatorUtil.html#locate(java.lang.String, java.lang.String)

For example,

bookLocalService= com.liferay.portal.kernel.bean.PortletBeanLocatorUtil.locate("book-portlet", "com.sample.service.BookLocalService")

@Sébastien - Thanks for a great post! It's still very useful.
Posted on 12/21/15 5:09 PM in reply to Kashif Ayyaz Haider.