How do I update 2 custom Jira fields at a time?

Hi all,

Thanks for reading my query. And hope someone could help me with my trivial question…
I got 3 custom fields:

  • Issue category - which is a cascading field (customfield_10220)
  • Functional area - single picklist (customfield_10221)
  • Resolver group - single picklist (customfield_10222)

Based on the values selected in Issue category field, I want to populate the Functional area and Resolver group fields. I use IF-ELSE-THEN condition statements, but the THEN code block is throwing an error.

Can someone please help with my script below?

def customFields = get(“/rest/api/2/field”)
.asObject(List)
.body
.findAll { (it as Map).custom } as List

//this was added by the scriptrunner cloud example as a way to check the listener is only applied to the correct project
def projectKey = “TEST”
if (issue == null || ((Map) issue.fields.project).key != projectKey) {
logger.info(“Wrong Project ${issue.fields.project.key}”)
return
}

def CategoryCfId = customFields.find { it.name == ‘Issue category’ }?.id
def FuncAreaCfId = customFields.find { it.name == ‘Functional area’ }?.id
def ResolverCfId = customFields.find { it.name == ‘Resolver group’ }?.id
def IssueCategory = issue.fields[CategoryCfId] as Map
def Resolver = issue.fields[FuncAreaCfId] as Map
def Resolver = issue.fields[ResolverCfId] as Map
logger.info(“Current Resolver group: ${Resolver.value}”)

def category = issue.fields.customfield_10220[‘value’].toString()
def subcategory = issue.fields.customfield_10220[‘child’][‘value’].toString()

logger.info (“Sub-category: ${subcategory}”)
if (category == “Non-SAP” && subcategory == “Jira”) {
Func_area = “Technology”
Resol_grp = “Jira Administrator”
} else if (category == “Technology” && subcategory == “Hardware”){
Func_area = “Technology”
Resol_grp = “IT Service Desk”
}

put(“/rest/api/2/issue/${issue.key}”)
.header(“Content-Type”, “application/json”)
.body([
fields: [
“Resolver group” : Resol_grp
“Functional area” : Func_area
]
])
.asString()
}

Regards,

And the error is? Which line?

Hi kcwong,

the error is on at line 38, column 25

2023-12-13 22:00:38.363 ERROR - startup failed:
Script1.groovy: 38: Unexpected input: ‘(“/rest/api/2/issue/${issue.key}”)\r\n .header(“Content-Type”, “application/json”)\r\n .body([\r\n fields: [\r\n “Resolver group” : Resol_grp\r\n “Functional area”’ @ line 38, column 25.
“Functional area” : Func_area
^
It is the second field I wanted to update, on this code block:
put(“/rest/api/2/issue/${issue.key}”)
.header(“Content-Type”, “application/json”)
.body([
fields: [
“Resolver group” : Resol_grp
“Functional area” : Func_area //<- thowing an error
]
])

Thanks for your reply.
Cheers.,

I think you have an array but no comma between the elements.

fields: [
“Resolver group” : Resol_grp  <-- No delimiter here
“Functional area” : Func_area
]

Thanks, @kcwong ,

Added a comma as suggested. The warning icon (exclamation point in yellow triangle) disappeared. But another warning came up at the very last line:
scriptrunner5

And with it, an error:

2023-12-14 00:28:56.808 ERROR - startup failed:
Script1.groovy: 42: Unexpected input: ‘([\r\n fields: [\r\n “Resolver group” : Resol_grp,\r\n “Functional area” : Func_area\r\n ]\r\n ])\r\n .asString()\r\n}’ @ line 42, column 1.
}
^

1 error

2023-12-14 00:28:56.812 ERROR - Class: com.adaptavist.sr.cloud.events.WebhookExecution, Config: null

Your variables Func_area and Resol_grp are defined in the scope of the if statement, you can’t use them outside.

Hi @kcwong ,

I really appreciate your quick response.

How do you suggest I achieve this? I basically wanted to first validate the values of Issue category and sub-category selected by the user on create or edit event using an IF/ELSE statement (I don’t know how to use SWITCH statement). Then once validated, at the end I want to automate the population of Functional area and Resolver group, hence the code is outside of IF/ELSE statement.

I would appreciate your kind help and guidance.

Regards,