Abbreviation used in the document:
Abbreviation |
Description |
---|---|
GUI |
Graphical User Interface |
BDA |
Business Data Attribute |
BRA |
Business Relation Attribute |
The following terms and phrases are commonly used in the context of Groovy Service. Each of them is followed by a brief description and it may include a link to more in-depth documentation of that subject.
Term / Phrase |
Description |
---|---|
Groovy Script |
A relatively small piece of code, written in Apache Groovy language, that implements customizable business logic within the Digital Sales Platform. |
Extension Point |
Predefined points where a Groovy Script may be injected, to alter or enhance its default behavior. |
Binding Variables |
Variables that are automatically made to be available inside the script. Depending on the extension point, different variables will be available. |
BDA |
An attribute that has primitive value(s), e.g. string, number, or date. |
BRA |
An attribute that has other complex object(s) as its value(s). |
Groovy Scripts in Digital Sales Platform
The Digital Sales Platform supports scripting using the Apache Groovy language. The groovy script can be executed automatically when a particular supported action (called extension point) is performed, for example when the user created a Quote or updated the value of a field. Details about each extension point will be given in the rest of this document.
Generally, there are two use cases for scripts in the application:
- It can be used for creating custom functionality in the system. For example, you may discover that there are some sets of actions that you wish to perform repeatedly and automatically. As these actions may be quite specific to your workflow or company, it would not make sense for such functionality to be implemented in the base software. Instead, this can be done using a custom script.
- It can be used for features whose usage depends on the specific business logic that will be unique for each company. This business logic can be encapsulated within a custom script.
In all cases where a feature is developed using the groovy script, the feature is treated as a custom program and hence its maintenance falls on the customer system.
Enable Groovy Background Task
A variety of tasks can be set up to run regularly in the background where the Groovy scripts run does not require any input for Accounts and others. In the Digital Sales Platform, select 'System Processes' > 'System Tasks' > 'Master Data' > 'GroovyTask'
Select “Groovy Job” under Task Resource, then select the groovy script to be run.
Then, in the Digital Sales Platform, select 'Administration' > 'Groovy Script' > 'Background Job'. Under the 'Type' section, select 'Allows to run this Groovy Script as System Task'.
Groovy
Apache Groovy is a programming language that is often used for scripting, designed for the Java platform. Any java code can be used as a groovy script, but it also has a concise, readable, and easy-to-learn alternative syntax. Thus, it is easy for both experienced Java programmers as well as newcomer to use it. It is dynamically compiled to Java virtual machine (JVM) bytecode and interoperates seamlessly with other Java code and libraries used by the application.
A Groovy script can have any file extension, but by convention, it should be given the “groovy” file extension. As the script will be compiled into a Java-compatible class during runtime, the filename should also meet the criteria of a Java class. Please refer to official Java documentation (such as https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html) for the details on this restriction.
Tools
Any text editor or IDE (Integrated Development Environment) can be used to develop the groovy script. However, note that at this point there is no support for any IDE to allow for automatic code completion for the custom classes. If you intend to do cloud debugging (see section 5 Debugging Groovy Scripts), you will need an Eclipse IDE.
API Documentation
Please refer to http://groovy-lang.org/ for further information and documentation about writing groovy scripts in general. Please refer to https://in-mind-cloud.github.io/content/groovy-doc/ for the Javadoc for the custom classes and available API.
Binding Variables
Depending on the extension point, certain variables are automatically made to be available inside the script. These are called binding variables. Some binding variables are available to all scripts, while others are only available to specific extension points. For example, extension points related to Quote will automatically have a variable representing the Quote’s data called salesItemsTree. The script can immediately make use of this variable to query information or make changes to the Quote and its sales items.
The list of binding variables that are available for each extension point is described within this document. However, it is also possible to get the list of available binding variables within the script itself. As an example, the following script will obtain the class and name for all available binding variables and output it to a notification message.
def sb = new StringBuilder();
binding.variables.each{
sb.append('(' + it.value.getClass().getName() + ') ')
.append(it.key)
.append("\n");
}
groovyLogger.notifyInfo(sb.toString());
Common Binding Variables
The following are the common binding variables available to all extension points:
Variable Name |
Class |
Description |
---|---|---|
dbLookupTable |
GroovyLookupTable |
For accessing Lookup Tables that the script has been granted access to, if any. |
groovyCtxUtil |
GroovyCtxUtil |
Utility class for ad-hoc data access. |
groovyLogger |
GroovyLogger |
For logging into the log file and for displaying notifications in the GUI. |
sessionLanguage |
String |
The current session’s language code. |
userInfo |
UserInfo |
The current user’s basic information. This variable will be null if the script is not triggered under any particular user’s session, for example if the script is triggered by a scheduled system task. |
currencyCollection |
CurrencyCollection |
The collection to store all the Currency information as a POJO format |
uomCollection |
UnitOfMeasureCollection |
A collection to store all the Unit Of Measure information as a POJO format |
Uploading a New Groovy Script
- Login to ISS with administrator right, go to Administration > Master Data Management. Search with groovy and then selects the Groovy Script.
- The list of currently existing Groovy Scripts will be shown.
- For some extension points, there can only be one script for it in the system, which would have been pre-created, and thus you can only edit the existing Groovy Script. For other extension points, you can either create a new Groovy Script or edit existing ones.
To create a new Groovy Script, click on the Plus button. A new empty row will be added at the top of the list.
- Click on the relevant groovy script to edit its data. The relevant fields are:
Field Name |
Description |
---|---|
Name |
The object name of the Groovy Script |
Type |
The type of groovy script. One script type generally corresponds to one extension point. The exception is the “System Used” script type, which is used for many extension points with predefined scripts.
|
Script File |
The physical groovy script file. Click on the folder icon to open the file selection window and select the file to upload. Only files with “.groovy” extension will be accepted. Once the file is selected, the “Upload” button will be shown. Click on that button to upload the file to the server. If a file has been uploaded, the name of the file is shown below the uploader. Clicking the file name will download the existing script. |
Base DWG File |
This option will only be shown when the selected Type is “Generate Autocad Drawing”. This is the base file that will be used to generate the drawing. |
Lookup Tables |
The list of Lookup Tables that the script will be allowed to access. Both read and write access will be granted. |
Note: Groovy scripts can be enabled to run as a system task in the background