This module was created as a proof of concept (for embedding Groovy into OpenMRS) and to serve as a base module for other modules that want to use Groovy scripting as well.
For a quick overview of some of the cool groovy features, see this spring source training video
Find and click on the Groovy Scripting Form link within the Groovy Module section of OpenMRS' administration page. You should be taken to a simple web form with a single textarea followed by a "GO" button. Type any valid Groovy script into the textarea and click the button to execute the script.
Some variables are automatically provided (most of these represent the API service):
- fn (as of 2.2)
fn variable provides access to convenience functions:
) from users")
Runs the given SQL and returns the result set (only allows reading from database)
fn.sql("update users set name='Super-duper User' where user_id = 1", false)
Runs SQL that can write to the database
Scheduling Groovy Scripts
As of version 2.2, you can schedule Groovy scripts to run automatically using the scheduler.
- Create and save a groovy script
- Create a task (Scheduler within administration page) using the Schedulable Class:
- Add a property as follows:
- name: "script name" (the literal text, 'script name' without quotes... not the name of your script)
- value: the name of your script
Accessing Other Module Services
When trying to access the service of another module within a Groovy script, you might see a an error like:
If you do, try loading the service like this:
- Reference: developer mailing list discussion
Groovy script examples
List 100 concepts?
Display some encounters?
Display some observations
Search for patients by name
Create providers for all users
Create providers for users with specified roles
Execute SQL commands
Using groovy only:
Print user and patients
Delete duplicate patients
Modify observation to use coded answer
Delete concept and concept answers
Change program and workflow
Or this simple script
Change form id
This used when we duplicate an InfoPath form into htmlform, and want to change all the infopath forms to use htmlform view/edit capabilities.
Delete forms (with no encounters)
This is useful when no birthdate is known and bogus ages are selected (ie. 106 years old)
Print concept sets and names
Void concept names
Change concept_map comment (for OpenMRS 1.9 upgrade)
A more complex example
Trigger Sync for a Patient (may not be complete)
Make REST web service calls
Make sure you don't save your password in a script! You can enter your password into this script and run it, but make sure to remove your password before saving the script!
The following script sets up basic authentication, makes a call to the REST Web Services module, and then displays the result.
Copy the JSON-formatted result into an editor like Sublime Text 2 with a JSON formatter (in Sublime Text 2, Tools > Command Palette > Package Control : Install Package > Pretty JSON) to quickly format the JSON into a more legible form.
Look for HTML Forms that have the same concept repeated, without extra contextual information
Update a Person Attribute and insert an Obs given a value from another Obs
A slightly groovier way
java.lang.ClassFormatError: Invalid method Code length 82342 in class file Script1
Solution: Java only allows methods that are 64K long. Reduce the size of your groovy script.