liam_davison
I'm new here

Script or Module?

Jump to solution

Hello developers,

When do you write a script, and when do you write a module running on the server? I have written several long scripts and I am beginning to think they should be modules on the server instead. I'm expecting that modules written in Java will run faster than Beanshell scripts. I think it would also be easier to write good object-orientated code in Java, with the support of an IDE like Eclipse or IntelliJ.

If I do start doing this - how do I call my module from an FS_BUTTON? How are parameters passed down? Is context.getElement() the same in web client as it is on the server? Can a module interact with the web client, for instance triggering a navigation from one page to another?

I have attached a sample script that I'm thinking of moving to a server on the module.

It creates a copy of an existing page, and updates various links and form elements on both the original and new page.

I know this is a vague subject - I was just hoping for some ideas and inspiration!

With thanks,

Liam Davison

1 Solution

Accepted Solutions
sebastianc
Crownpeak employee

Hello Liam,

do you need further help or did Michael's reply already help you? If so, it would be great if you marked his reply as "correct answer" so that other community users find the solution easily.

Best regards,

Sebastian

View solution in original post

0 Kudos
2 Replies
mbergmann
Crownpeak employee

Hello Liam,

the type (module or script) has nothing to do with the location where the code is executed. Both script and module code can be executed in the client or on the server. It only depends on the context - i.e. how and where the start is triggered. Despite modules are always installed on the server, the client can load them from there and execute the code locally.

In general, we strongly recommend modules instead of beanshell scripts for multiple reasons:

  • Scripts can only manually be checked for changed API as they are evaluated at runtime. If e.g. at some time a class or method is removed from the API, you only get aware of this when the script runs AND the concrete line of code is really executed.
  • Modules: Full IDE support with code completion, syntax checks, auto imports, unit tests, refactoring etc.
  • Management of code using VCS
  • The risk of typos is very high in scripts, there is no check for misspelled variables, methods, classes or forgotten parameters or wrong types. Errors will only occur (and get visible) when the line is executed.
  • You cannot create re-usable elements
  • Only very basic support of java generics
  • Slower execution
  • etc.

Technically you don't "execute a module" but more precisely one of the module's components. One common component type is a class implementing the Executable interface and declared as <public> component. Such Executables can then be used in FS_BUTTONs. The Executable interface defines an "execute" method you will have to implement. Here, you get a parameter map of type Map<String, Object>. You can acces the context object using the key "context" from that Map.

In some cases (e.g. workflow scripts, (context) menu scripts) you need a "script object". But this can directly delegate to an Executable in a module. Such a script contains only two lines:

#!executable-class

NameOfTheExecutablesPublicComponent

For more information, you should have a look at the plugin development topics in the ODFS and especialy the examples.

Michael

sebastianc
Crownpeak employee

Hello Liam,

do you need further help or did Michael's reply already help you? If so, it would be great if you marked his reply as "correct answer" so that other community users find the solution easily.

Best regards,

Sebastian

0 Kudos