---
title: "Shared Groovy Scripts"
canonical: "https://support.appfire.com/space/JMWE/462061611/Shared%20Groovy%20Scripts"
format: markdown
---
> Macro (aura-html)

<span style="color: #172b4d">This document details the Shared Groovy scripts feature of JMWE. Using this </span><span style="color: #172b4d">you can define global</span><span style="color: #172b4d"> Groovy scripts that will be available from any Groovy script written in the Groovy console, post-functions, conditions and validators.</span><span style="color: #172b4d"> </span><span style="color: #091e42">This is useful for reusing complex/lengthy scripts across multiple workflow transitions. </span><span style="color: #172b4d">It is available under </span>[<span style="color: #172b4d">JMWE administration pages</span>](https://appfire.atlassian.net/wiki/spaces/JMWE/pages/461701298)<span style="color: #172b4d">. </span>

<span style="color: #172b4d">**On this page:**</span>


## <span style="color: #172b4d">Create a Shared Groovy Script</span>

<span style="color: #172b4d">To create a shared Groovy script:</span>

1. <span style="color: #172b4d">Go to </span>[<span style="color: #172b4d">JMWE administration pages</span>](https://appfire.atlassian.net/wiki/spaces/JMWE/pages/461701298)
2. <span style="color: #172b4d">Click on Shared Groovy Scripts</span>
3. <span style="color: #172b4d">Click on </span>`New shared script`
4. <span style="color: #172b4d">Enter the name of this shared script in </span>`Shared script name`<span style="color: #172b4d">. This name can be used as the name of the </span>`Class`<span style="color: #172b4d"> that will contain all methods and data members defined in the script.</span>
5. <span style="color: #172b4d">Enter an optional description in </span>`Description`<span style="color: #172b4d">.</span>
6. <span style="color: #172b4d">Input a valid Groovy script in </span>`Groovy script`<span style="color: #172b4d">. </span>:warning:<span style="color: #172b4d"> It is strongly recommended to wrap any (static) method in a class.</span>
7. <span style="color: #172b4d">Click on </span>`Save`<span style="color: #172b4d">.</span>

## <span style="color: #172b4d">Edit a Shared Groovy Script</span>

<span style="color: #172b4d">To edit a Shared Groovy script:</span>

1. <span style="color: #172b4d">Go to </span>[<span style="color: #172b4d">JMWE administration pages</span>](https://appfire.atlassian.net/wiki/spaces/JMWE/pages/461701298)
2. <span style="color: #172b4d">Click on </span>`Shared Groovy Scripts`
3. <span style="color: #172b4d">Click on </span>`Edit`<span style="color: #172b4d"> for the specific Shared Groovy Script</span>
4. <span style="color: #172b4d">Modify the script.</span>
5. <span style="color: #172b4d">Click on </span>`Save`<span style="color: #172b4d">.</span>

## <span style="color: #172b4d">Delete a Shared Groovy Script</span>

<span style="color: #172b4d">To delete a Shared Groovy script:</span>

1. <span style="color: #172b4d">Go to </span>[<span style="color: #172b4d">JMWE administration pages</span>](https://appfire.atlassian.net/wiki/spaces/JMWE/pages/461701298)
2. <span style="color: #172b4d">Click on </span>`Shared Groovy Scripts`
3. <span style="color: #172b4d">Click on </span>`Delete`<span style="color: #172b4d"> for the specific Shared Groovy Script</span>
4. <span style="color: #172b4d">Confirm the action.</span>
5. <span style="color: #172b4d">The Shared Groovy Script gets deleted.</span>

> ⚠️ It is strongly recommended to remove the references to the Shared Groovy Script in any workflow extension before deleting it.

## Shared Groovy Scripts best practices

<span style="color: #333333">Defining a static method</span>

<span style="color: #333333">In this example, we will create a simple static method </span>`plusOne`<span style="color: #333333"> in a shared Groovy Script named </span>`Functions`. Note, the name of the shared script becomes the name of a class.

```groovy
static int plusOne(int i) {
  return i+1;
}
```

You can use this in a Groovy script section of any workflow extension (e.g. a Scripted Groovy Operation post-function) like this:

```groovy
return Functions.plusOne(1)
```

<span style="color: #172b4d">When you test this script in the Groovy editor against any issue the tester will return </span>`2`

### <span style="color: #333333">Defining an explicit class</span>

<span style="color: #333333">In this example, we will create a shared script named </span>`GreetMe `<span style="color: #333333">with the script:</span>

```groovy
class GreetMe{
  String aString = "Hello"
  String sayIt() {
    return aString;
  } 
}
```

<span style="color: #172b4d">You can use this in a Groovy script section of any workflow extension (e.g. a Scripted Groovy Operation post-function) like this:</span>

```groovy
def obj = new GreetMe()
return obj.sayIt()
```

<span style="color: #172b4d">When you test this script in the Groovy editor against any issue the tester will return </span>`Hello`

### <span style="color: #333333">Defining multiple classes inside a single shared script</span>

<span style="color: #333333">In this example, we will create a shared script named </span>`MyClasses`<span style="color: #333333"> with the script:</span>

```groovy
interface Animal {
  String getName()
}

class Cat implements Animal {
  String getName() {
    return "Cat"
  }
}

static String exec() {
  return new Cat().name
}
```

<span style="color: #172b4d">You can use this in a Groovy script section of any workflow extension (e.g. a Scripted Groovy Validator) like this:</span>

```groovy
return new Cat().name == MyClasses.exec()
```

<span style="color: #172b4d">When you test this script in the Groovy editor against any issue the tester will return </span>`true`

### <span style="color: #333333">Defining a service with access to global variables and functions</span>

<span style="color: #172b4d">The global variables and functions that can be used in any Groovy script defined in a workflow extension are </span>*<span style="color: #172b4d">not</span>*<span style="color: #172b4d"> available directly in shared Groovy scripts. The easiest approach to be able to access them is to define a class that will contain all the desired methods (non-static) and to pass the globals to that class. This will be like a "Service". For example:</span>

```groovy
import com.atlassian.jira.issue.IssueManager

class MyService {
  private Script globals;
  
  public MyService(Script globals) {
    this.globals = globals;
  }
  
  public String getIssueKey() {
    return globals.issue.key;
  }

  public Issue getIssue(String key) {
    return globals.getComponent(IssueManager).getIssueObject(key)
  }
}
```

<span style="color: #172b4d">And you can then use this "Service" from the main script:</span>

```groovy
MyService utils = new MyService(this)

utils.issueKey //calls the getIssueKey() method, which accesses the issue global variable
utils.getIssue("TEST-1") //calls the getIssue method which accesses the global getComponent function
```

## Use cases for Shared Groovy scripts

### Add Days to Date excluding weekends

Create a shared Groovy script that adds a certain number of days to a date excluding weekends

1. Create a shared groovy script, `AddDaysExcludingWeekends`, with the following script
2. Save it.

You can use this in a post-function to set <span style="color: #333333">a Date-time picker field. For example Set a Date time picker field to the Due date plus 5 days, excluding weekends. The script in the post-function will be:</span>

```groovy
return AddDaysExcludingWeekends.addWorkingDays(issue.duedate,5)
```

### Check for attachments with a specific extension during a transition

Create a shared validator that checks for <span style="color: #172b4d">attachments of a specific extension were added to the transition screen</span>

1. <span style="color: #091e42">Create a shared groovy script, </span>`AttachmentValidator`<span style="color: #091e42">, with the following script:</span>
2. <span style="color: #091e42">Save it.</span>

<span style="color: #091e42">Now you can use this in Scripted Groovy validator across transitions to validate the attachments added on the transition screen. Add the Scripted Groovy validator and call the method passing the </span>`issue `<span style="color: #091e42">variable and extension as parameters.</span>

```groovy
AttachmentValidator.checkAttachments(issue,".pdf")
```