Need help on copying custom field to another custom field

Hi all,

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”)

  •    .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()*
    

But the Approvers field is not being updated at all.
Appreciate any help/guidance fixing my script to work.

Regards,

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]

I hope this helps.
Thanks, Ivan.

Hi (@imadero) Ivan,

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.

Cheers

Hi Ivan (@imadero ),

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?

Regrads,
Zaldy

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

Thanks, Ivan.

Hi Ivan,

I tried your suggestion:

//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.

Regards,

Hi, looks like it was a bit tricky.

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

Thanks, Ivan

You’re such a legend, Ivan.
The ${ApproversCfId} did the trick.
I simply copied-pasted your latest code.
And it worked as expected.

I really appreciate this very much. :smiley:
Cheers, mate.

Just one more thing…

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.

Glad to be able to help!
To accomplish the validation what I would do here is to check the following:

if(issue.fields.issuetype.name != 'Service Request with Approval'){
    return
}

The return statement makes the script stop executing when the issue type is different from the one you choose.

Thanks, Ivan.,

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.