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

Need help on Jira Cloud ScriptRunner

Zaldy Parian
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.
December 19, 2023

Hi all,

We're using JSM on cloud and exploring ScriptRunner to update 2 fields based on the selection on a cascading custom field. 

I started with this code in Script Console...


// test to update Resolver group and Functional area

def issueKey = 'TEST-37'

// Fetch the issue object from the key
def result = get("/rest/api/2/issue/${issueKey}")
.header('Content-Type', 'application/json')
.asObject(Map)
.body
.fields

//def fields = issue.fields as Map
Category_parent = result.customfield_10220.value
Category_child = result.customfield_10220.child.value
logger.info ("Parent value: ${Category_parent}")
logger.info ("Child value: ${Category_child}")
//def Category_parent = 'Issue category'.value

if (Category_parent == "Application (Non-SAP)" && 'Category_child' == "Jira") {
def func_area = "Technology"
def resol_grp = "Jira Administrator"
}
else if (Category_parent == "Enterprise Data Management" ) {
def func_area = "Enterprise Data Management"
def resol_grp = "EDM"
}
else if (Category_parent == "Application (SAP)" && Category_child == "OpenText" ) {
def func_area = "Corporate"
def resol_grp = "SAP Finance Management"
}
else if (Category_parent == "Warehouse Management" && Category_child == "Dematic" ) {
def func_area = "Supply Chain"
def resol_grp = "SAP Warehouse Management"
}
else { //it always end up here without processing the update on the fields below
def func_area = "Technology"
def resol_grp = "IT Service Desk"
return "No updates done"
}

put('/rest/api/2/issue/' + issueKey)
.header('Content-Type', 'application/json')
.body([
fields:[
"Functional area": func_area,
"Resolver group" : resol_grp
]
])
.asString()
logger.info ("Functional area: ${func_area}")
logger.info ("Resolver group: ${resol_grp}")


My script above always end up in the last ELSE block and will not process the PUT (or update) block. 

What am I doing wrong? Please help.

Regards,

 

1 answer

2 votes
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 20, 2023

Hi Zaldy,

 

I can confirm that your script has a return block in the final, and the way ScriptRunner for Jira Cloud works is that when a return statement is hit, it will stop the whole script, and this will be the reason the put request below it does not get executed.

To solve this, you should either replace the return statement for a log statement or move the put logic above the return statement in the code.

I hope this helps.

Regards,

Kristian

Zaldy Parian
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.
December 21, 2023

Hi @Kristian Walker _Adaptavist_ ,

First of all, thanks for your reply and suggestion. I do always take note of your responses from other postings/quesitons in this Community with regards to ScriptRunner.

I replaced the 'return' logic with logger.info statement. However, the 'put' logic is still not happening. 

I got this from the Logs tab:

Serializing object into 'interface java.util.Map'
GET /rest/api/2/issue/TEST-37 asObject Request Duration: 1488ms
Parent value: Application (Non-SAP)
Child value: Jira
No updates done
No such property: func_area for class: Script1 on line 41
Class: com.adaptavist.sr.cloud.events.ConsoleScriptExecution, Config: [userTriggered:true]

Line 41 is where the start of the 'put' statement.

Happy Holidays.

 

Cheers,

 Line 41 is where the start of the 'put' statement.

Happy Holidays.

 

Cheers,

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 28, 2023

Hi Zaldy,


Can you try moving the put statement above the logger.info statement, so that is inside the else statement to see if this triggers it.

I believe the issue is you set the variable named func_area on line 36 which is inside the else statement then your put request is outside this so this property does not exist when the put request tries to reference it.

Also the same is true for the variable resol_group defined on line 37.

You either need to move the put inside the else where these are defined or define these variables above the put request outside the else statement.

I hope this helps and happy holidays.

Kristian

Zaldy Parian
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.
January 2, 2024

I see. Thanks for the advice.

I made the modification as...

 

def func_area
def resol_grp

if (Category_parent == "Application (Non-SAP)" && Category_child == "Jira") {
func_area = "Technology"
resol_grp = "Jira Administrator"
} else if (Category_parent == "Enterprise Data Management") {
func_area = "Enterprise Data Management"
resol_grp = "EDM"
} else if ((Category_parent == "Application (SAP)" && Category_child == "OpenText") {
func_area = "Corporate"
resol_grp = "SAP Finance Management"
} else {
func_area = "Technology"
resol_grp = "IT Service Desk"
}

 

And the put statement is....

 

def update = put("/rest/api/2/issue/${issueKey}")
.header("Content-Type", "application/json")
.body([
fields: [
"customfield_10221" : func_area?.toString(), 
"customfield_10222" : resol_grp?.toString() 
]
])
.asString()

 

Somehow, I get a warning of "Unexpecte input: 'else' where my else statement starts.

I tried to ignore it but after running, it resulted to an error....

startup failed:
Script1.groovy: 35: Unexpected input: 'else' @ line 35, column 3.
} else {
^

1 error

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 3, 2024

Hi Zaldy,

 

This will likely be because you have a closing } bracket before the else statement that needs moving down in the script.

Regards,

Kristian

Zaldy Parian
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.
January 3, 2024

Hi @Kristian Walker _Adaptavist_ ,

 

I tried several ways to reposition the opening and closing brackets { } around the IF statement. I was able to get rid of the warning.

However, in the put statement, it resulted to an error :-( 

PUT /rest/api/2/issue/TEST-37 asString Request Duration: 367ms
PUT request to /rest/api/2/issue/TEST-37 returned an error code: status: 400 - Bad Request
body: {"errorMessages":[],"errors":{"customfield_10221":"Specify a valid 'id' or 'name' for Functional area","customfield_10222":"Specify a valid 'id' or 'name' for Resolver group"}}
Serializing object into 'interface java.util.Map'

Any idea?

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 4, 2024

Hi Zaldy,

This error indicates the fuleds being updated cannot be resolved.

As the error indiactes they have the ids of customfield_10222 and customfield_10221 so you should reference these as the name of the fields in the put request by  making it similar to below.

put('/rest/api/2/issue/' + issueKey)
.header('Content-Type', 'application/json')
.body([
fields:[
"customfield_10221": func_area,
"customfield_10222" : resol_grp
]
])
.asString()

Otherwise, you need to look up the fields Id's by name to reference them by name like shown in the docs here.

I hope this helps.

Regards,

Kristian

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events