Running Groovy Scripts in the SIL Manager

Method 1 - Run Groovy Script Inline

The advantage of this method is that the script is in the same file. 

To escape a character, use a backwards slash before the character. For example, the following quote will be interpreted by the SIL Engine as a string literal: \"


Get Custom Field Value
string issue = "DEMO-1";
string cfName = "textField";

function getCFValue() {
    string groovyscript = 
        "import com.atlassian.jira.component.ComponentAccessor \n" +
        "import com.atlassian.jira.issue.fields.CustomField \n" +
        "import com.atlassian.jira.issue.CustomFieldManager \n" +
        "import com.atlassian.jira.issue.Issue \n" +
        "import com.atlassian.jira.issue.IssueManager \n" +
        
        "def cfManager = ComponentAccessor.customFieldManager \n" +
        "IssueManager issueManager = ComponentAccessor.getIssueManager() \n" +
        "def issue = issueManager.getIssueObject(\"" + issue + "\") \n" +
        "def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName(\"" + cfName + "\") \n" +
        "def requestType = issue.getCustomFieldValue(cf) \n" +
        "return requestType \n" +
    "";
    return executeGroovyScript(groovyscript);
}

runnerLog(getCFValue());


Method 2 - Run Groovy Script From a Separate File

The advantage of this method is that it is easier to read. In the example below, the SIL file and the Groovy file are intended to be used together.

Replace the variables in the groovy script by using the replace() routine.


SIL File
string issue = "EX-1";
string cfName = "textField";
function getCFValue() {
    string groovyscript = readFromTextFile("Groovy/getCfValue.groovy");
    groovyscript = replace(groovyscript, "$issue$", issue);
    groovyscript = replace(groovyscript, "$cfName$", cfName);
    return executeGroovyScript(groovyscript);
}
runnerLog(getCFValue());
Groovy File
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
def cfManager = ComponentAccessor.customFieldManager
IssueManager issueManager = ComponentAccessor.getIssueManager()
def issue = issueManager.getIssueObject("$issue$")
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("$cfName$")
def requestType = issue.getCustomFieldValue(cf)
return requestType