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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root


1 badge earned


Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!


Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.


Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!


Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
Community Members
Community Events
Community Groups

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
Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
Feb 01, 2018

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
AUG Leaders

Atlassian Community Events