My first post here. Being new to ScriptRunner, I wanted to create couple of scripts as replacement for our Jira Service Management automation rule.
The first requirement is:
One create or edit, update the Approvers field with the value from AD Manager field
Approvers field is a multi users pick list field; while AD Manager is a single user pick list field. And AD Manager is automatically updated from our Active Directory system.
From the sample scripts and postings, I come up with this script⌠// get custom fields so we can find the id of AD Manager field def customFields = get(â/rest/api/2/fieldâ)
//to check the listener is only applied to the correct project def projectKey = âTESTâ //update to match the expected project if (issue == null || ((Map) issue.fields.project).key != projectKey) {
Hi.
At first, in the editor, there is an option to paste code that makes the review easier and looks like that:
// get custom fields so we can find the id of AD Manager field
def customFields = get(â/rest/api/2/fieldâ)
.asObject(List)
.body
.findAll { (it as Map).custom } as List<Map>
def ADManagerCfId = customFields.find { it.name == âAD Managerâ }?.id
def ApproversCfId = customFields.find { it.name == âApproversâ }?.id
//to check the listener is only applied to the correct project
def projectKey = âTESTâ //update to match the expected project
if (issue == null || ((Map) issue.fields.project).key != projectKey) {
logger.info(âWrong Project ${issue.fields.project.key}â)
return
}
//get the values from your issues
def ADManager = issue.fields[ADManagerCfId] as Map
def ApproversName = issue.fields[ApproversCfId] as Map
//update the issue with the new approver
put(â/rest/api/2/issue/${issue.key}â)
.header("Content-Type", "application/json")
.body([
fields: [
"Approvers": ADManager
]
])
.asString()
Also, regarding your problem, the users, usually are updated using the accountId, so you should use ADManager.accountId
And also, if itâs a list, you should send an array
"Approvers: [ADManager.accountId]
Thanks for your suggestion. It was my first post and not aware there is a better to copy-paste the code. I noticed it after I posted my question that the code is not 100% readable.
Iâll try the suggested code and will get back to you soon after.
I modified my put statement adding [ADManager.accountId], as inâŚ
//update the issue with the new approver
put("/rest/api/2/issue/${issue.key}")
.header("Content-Type", "application/json")
.body([
fields: [
"Approvers": [ADManager.accountId]
]
])
.asString()
After editing the Jira issue, the log showed an errorâŚ
2023-11-21 22:57:33.240 INFO - Serializing object into âinterface java.util.Listâ
2023-11-21 22:57:33.241 INFO - GET /rest/api/2/field asObject Request Duration: 499ms
2023-11-21 22:57:33.665 INFO - PUT /rest/api/2/issue/TEST-15 asString Request Duration: 406ms
2023-11-21 22:57:33.665 WARN - PUT request to /rest/api/2/issue/TEST-15 returned an error code: status: 400 - Bad Request
body: {âerrorMessagesâ:,âerrorsâ:{âApproversâ:âField âApproversâ cannot be set. It is not on the appropriate screen, or unknown.â}}
2023-11-21 22:57:33.665 INFO - Serializing object into âinterface java.util.Mapâ
2023-11-21 22:57:33.666 ERROR - Please use the ScriptRunner user to complete this task not the Initiating User.
See https://docs.atlassian.com/jira/REST/cloud/#api/2/issue-editIssue for more information.
If you are using the ScriptRunner user then check the Field Configuration
The script is set to run as ScriptRunner Add-on User.
What am I doing wrong?
Hi.
I think the problem is âApproversâ is the name of the field, not the id, and you should send the id of the field to update â ApproversCfId : ADManager.accountId
Also check if Approvers is a list or not, if not a list you should send only âApproversâ: ADManager.accountId
//update the issue with the new approver
put("/rest/api/2/issue/${issue.key}")
.header("Content-Type", "application/json")
.body([
fields: [
*ApproversCfId: [ADManager.accountId]*
]
])
.asString()
But I got this error in the logâŚ
2023-11-23 03:13:37.798 INFO - Serializing object into âinterface java.util.Listâ
2023-11-23 03:13:37.842 INFO - GET /rest/api/2/field asObject Request Duration: 1004ms
2023-11-23 03:13:38.454 INFO - PUT /rest/api/2/issue/TEST-14 asString Request Duration: 416ms
2023-11-23 03:13:38.454 WARN - PUT request to /rest/api/2/issue/TEST-14 returned an error code: status: 400 - Bad Request
body: {âerrorMessagesâ:,âerrorsâ:{âApproversCfIdâ:âField âApproversCfIdâ cannot be set. It is not on the appropriate screen, or unknown.â}}
2023-11-23 03:13:38.454 INFO - Serializing object into âinterface java.util.Mapâ
2023-11-23 03:13:38.456 ERROR - Please use the ScriptRunner user to complete this task not the Initiating User.
See https://docs.atlassian.com/jira/REST/cloud/#api/2/issue-editIssue for more information.
If you are using the ScriptRunner user then check the Field Configuration
The Approver field is a multi-value user picklist.
put("/rest/api/2/issue/${issue.key}")
.header("Content-Type", "application/json")
.body([
fields: [
"${ApproversCfId}": [ //You need here to interpolate ApproversCfId as a String
['accountId': ADManager.accountId]
]
]
])
.asString()
But this work for me in a test instance.
Also check Approvers field is well configured in the proper screens
How to I add an IF condition for issue type = âService Request with Approvalâ; ELSE do nothing or quit.
Currently the script is running even for Incident and Problem tickets.