Need help on Jira Cloud ScriptRunner
Asked by Zaldy Parian:

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,


Answers:
author: Kristian Walker _Adaptavist_
body: 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

comments: author: Zaldy Parian
body: Hi [@Kristian Walker _Adaptavist_](/t5/user/viewprofilepage/user-id/781617) ,

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,


author: Kristian Walker _Adaptavist_
body: Hi Zaldy,

<br />

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


author: Zaldy Parian
body: 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


author: Kristian Walker _Adaptavist_
body: 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


author: Zaldy Parian
body: Hi [@Kristian Walker _Adaptavist_](/t5/user/viewprofilepage/user-id/781617) ,

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?


author: Kristian Walker _Adaptavist_
body: 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](https://docs.adaptavist.com/sr4jc/latest/features/script-listeners/script-listener-examples#populate-a-user-picker-field-from-a-text-field-).

I hope this helps.

Regards,

Kristian


Tags: cloud

jira-service-management

jira-service-management-cloud

scriptrunner