Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal


  • Give kudos
  • Received
  • Given


  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

the variable issue is undeclared

We have installed the Adaptavist ScriptRunner for JIRA plugin. We would like to implement Behaviours Initialisers; however we find that code that we think should work results in static type checking errors. For example, this Behaviour serverside script has issues as it indicates:

"The variable [issue] is undeclared"

Our code is:


import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.project.Project
import groovy.json.*
import com.atlassian.jira.issue.index.IssueIndexManager

/* Set debugging level */

MutableIssue myIssue = (MutableIssue) issue
def MutableIssue issueObject = (MutableIssue) issue
def MutableIssue parentIssue = issueObject.getParentObject() as MutableIssue

/* Initialize global objects */
def customFieldManager = ComponentAccessor.getCustomFieldManager() //ComponentManager.getInstance().getCustomFieldManager()
def issueIndexManager = ComponentAccessor.getIssueIndexManager()

/* Define a table of issue type to Payer fields in that issue type */
def issueTypeToPayerFieldMap = [:]
issueTypeToPayerFieldMap["Miscellaneous Request"] = "Payer(s)"
issueTypeToPayerFieldMap["Operational Inquiry"] = "Payer(s)"
issueTypeToPayerFieldMap["Parser Test File Request"] = "Payer(s) for File Stream ID"
issueTypeToPayerFieldMap["Payment Inquiry"] = "Payer"
issueTypeToPayerFieldMap["Policy Inquiry"] = "Payer(s)"
issueTypeToPayerFieldMap["Policy Research Request"] = "Payer(s)"
issueTypeToPayerFieldMap["Project Request"] = "Payer(s)"
issueTypeToPayerFieldMap["Report Request"] = "Payer(s)"
issueTypeToPayerFieldMap["Test File Request"] = "Payer(s) for File Stream ID"
issueTypeToPayerFieldMap["Test Scenario Request"] = "Payer(s)"
issueTypeToPayerFieldMap["Raw CIT Payment Inquiry"] = "CIT_PAYER"
def payerFieldName = issueTypeToPayerFieldMap[]

/* Collect the Client Team Assignment URL for this environment from the Project Description field */
def parsedDescription
def projectDescription = issue.getProjectObject().getDescription()
log.debug("projectDescription:" + projectDescription)
if (projectDescription) {
parsedDescription = new JsonSlurper().parseText(projectDescription)
log.debug("parsedDescription:" + parsedDescription)
if (parsedDescription?.CTA_SERVICE_HOST) {

/* Retrieve list of user-selected Payers */
def payerFieldValue = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName(payerFieldName.toString()))
log.debug("payerFieldValue:" + payerFieldValue)
log.debug("payerFieldValue.getClass():" + payerFieldValue.getClass())

/* Turn it into a comma-delimited list for inclusion in the service call */
def commaDelimitedPayerList = ''
//Raw CIT Payment Inquiry payer value is a string, not a list, handle it differently
if (payerFieldValue.getClass() == String) {
commaDelimitedPayerList = '{"payerKeys":["' +payerFieldValue+'"]'+'}'
} else {
commaDelimitedPayerList ='{"payerKeys":' + new groovy.json.JsonBuilder(payerFieldValue).toString()+'}'
log.debug("commaDelimitedPayerList:" + commaDelimitedPayerList)

/* Make call to client team assignment service */
def serviceURLString = CTA_SERVICE_HOST + "/teamAssignmentService/rest/teamAssignments/getTeamAssignmentsForPayers"
log.debug("serviceURLString:" + serviceURLString)
def url = new URL(serviceURLString)
def connection = url.openConnection()
connection.doOutput = true
def writer = new OutputStreamWriter(connection.outputStream)
def rawServiceResponse = connection.content.toString()

/* Write the service response to the CLIENT_TEAM_SERVICE_RESPONSE field */
def responseField = customFieldManager.getCustomFieldObjects(issue).find { == "CLIENT_TEAM_SERVICE_RESPONSE"}
IssueChangeHolder changeHolder = new DefaultIssueChangeHolder()
responseField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(responseField), rawServiceResponse), changeHolder)

/* Parse the JSON string into a groovy Map data structure for manipulation */
def parsedResponseMap = new JsonSlurper().parseText(rawServiceResponse.toString())
log.debug("Parsed JSON Response:" + parsedResponseMap)

/* stash missing JIRA users away for display/resolution */
def List missingUserList = []

if ( == "Payment Inquiry" || == "Raw CIT Payment Inquiry") {
/* Role: Client Policy Manager */
missingUserList.addAll(updateMultiUserField(parsedResponseMap, "role", "CPM", "CPM(s)"))
/* Role: Client Service Manager */
missingUserList.addAll(updateMultiUserField(parsedResponseMap, "role", "CSM", "CSM(s)"))
/* Role: Client Service Medical Doctor */
missingUserList.addAll(updateMultiUserField(parsedResponseMap, "role", "CSMD", "Client MD(s)"))
/* Role: Policy Inquiry Analyst */
// missingUserList.addAll(updateMultiUserField(parsedResponseMap, "role", "Policy Inquiry Analyst", "PIA(s)"))
/* Role: Policy Inquiry Analyst */
missingUserList.addAll(updateMultiUserField(parsedResponseMap, "role", "CSD", "CSD(s)"))
/* Role: Test Analyst Group */
missingUserList.addAll(updateMultiUserField(parsedResponseMap, "role", "Test Analyst Group", "TA(s)"))
/* Role: FBA */
missingUserList.addAll(updateMultiUserField(parsedResponseMap, "role", "FBA", "FBA(s)"))

/* Policy Research Request - NOTHING NEEDED */
/* Project Request - NOTHING NEEDED */
/* Misellaneous Request - NOTHING NEEDED */

/* save missing user list to JIRA */
def missingUsersField = customFieldManager.getCustomFieldObjects(issue).find { == "Users Not In JIRA"}
IssueChangeHolder missingUsersChangeHolder = new DefaultIssueChangeHolder()
missingUsersField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(missingUsersField), missingUserList.join(",")), missingUsersChangeHolder)

/* Store and Re-index to save changes to DB and in-memory storage */

function to set values of user custom fields in jira issue.
JSON Value
Field Name
def updateMultiUserField(serviceResponseMap, key, value, fieldName) {
def userUtil = ComponentAccessor.getUserUtil()
def localCustomFieldManager = ComponentManager.getInstance().getCustomFieldManager()

/* iterate thru the list of nodes mathing our query */
def matchingNodes = serviceResponseMap.findAll{it.get(key) == value}
def List<ApplicationUser> matchingUsers = []
def List<String> missingUsers = []
matchingNodes.userId.each {
log.debug(key + ":" + value + " Node Found: " + it)
currentUser = userUtil.getUserByKey(it.toLowerCase())
if (currentUser != null) {
} else {
log.debug(key + ":" + value + " AD User NOT Found in JIRA: " + it)
log.debug(key + ":" + value + " Users List: " + matchingUsers)

def fieldToUpdate = localCustomFieldManager.getCustomFieldObjects(issue).find { == fieldName}
IssueChangeHolder localFieldChangeHolder = new DefaultIssueChangeHolder()
if (fieldToUpdate.hasValue(issue)) {
log.debug(key + ":" + value + " has an existing value. Updating value.")
fieldToUpdate.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(fieldToUpdate), matchingUsers), localFieldChangeHolder)
} else {
if (matchingUsers) {
log.debug(key + ":" + value + " has NO value. Creating value.")
fieldToUpdate.createValue(issue, matchingUsers)

log.debug(key + ":" + value + " issue.getCustomFieldValue(fieldToUpdate): " + issue.getCustomFieldValue(fieldToUpdate))

/* return a list of users who were not found in JIRA */
return missingUsers

/* Re-set debugging level */

1 answer

1 vote

Behaviours do not have the issue variable. Instead you should use underlyingIssue, Kindly read the follwoing article:

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Trello

📹 What is a Trello board?

Hi, Atlassian Community! My name is Brittany Joiner and I am a Trello enthusiast and Atlassian Community Leader. I'll be sharing with you a series of videos with my top tips on how to best use Tre...

11 views 0 3
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you