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

Update linked issue field with scriptrunner

Ana Luiza Chagas September 23, 2024

Hey guys,

My team and I are looking for ways to develop automation using scriptruner for Jira Cloud to copy the value of a custom field in project A to the same field in the item linked to project B.

Has anyone seen something similar and could help me, please?

Thanks!

3 answers

Suggest an answer

Log in or Sign up to answer
0 votes
xelomax
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 1, 2024
Hello, with this script you could start to solve what you need, you would need to add any conditional that you want.
With this you can replace the labels with some custom field
This script checks if an issue has a linked issue and copies the labels from the source issue to the linked issue. It first retrieves the issue details, including any linked issues, then it copies the labels from the original issue to the linked issue, avoiding duplicates.

import groovy.json.JsonSlurper
def issueKey = "TEST-1" 

 

// Make a request to get issue details, including linked issues and labels
def issueResponse = get("/rest/api/3/issue/${issueKey}?fields=issuelinks,labels")
    .header('Content-Type', 'application/json')
    .asString()

 

// Check if the request was successful
if (issueResponse.status == 200) {
    def issueData = new JsonSlurper().parseText(issueResponse.body)
    def labels = issueData.fields.labels
    def issueLinks = issueData.fields.issuelinks

 

    // Check if there are any linked issues
    if (issueLinks) {
        issueLinks.each { link ->
            def linkedIssueKey = null

 

            // Determine the direction of the link (outward or inward)
            if (link.outwardIssue) {
                linkedIssueKey = link.outwardIssue.key
            } else if (link.inwardIssue) {
                linkedIssueKey = link.inwardIssue.key
            }

 

            if (linkedIssueKey) {
                logger.info("Linked issue found: ${linkedIssueKey}")
               
                // Make a request to get the labels of the linked issue
                def linkedIssueResponse = get("/rest/api/3/issue/${linkedIssueKey}?fields=labels")
                    .header('Content-Type', 'application/json')
                    .asString()

 

                if (linkedIssueResponse.status == 200) {
                    def linkedIssueData = new JsonSlurper().parseText(linkedIssueResponse.body)
                    def linkedLabels = linkedIssueData.fields.labels

 

                    // Combine the labels from the original and linked issue, ensuring no duplicates
                    def newLabels = (linkedLabels + labels).unique()

 

                    // Update the linked issue with the combined labels
                    def updateResponse = put("/rest/api/3/issue/${linkedIssueKey}")
                        .header('Content-Type', 'application/json')
                        .body([
                            fields: [
                                labels: newLabels
                            ]
                        ])
                        .asString()
                    if (updateResponse.status == 204) {
                        logger.info("Labels successfully copied to the linked issue: ${linkedIssueKey}")
                    } else {
                        logger.error("Failed to update labels in the linked issue: ${updateResponse.status}")
                    }
                } else {
                    logger.error("Failed to retrieve labels from the linked issue: ${linkedIssueResponse.status}")
                }
            }
        }
    } else {
        logger.info("No linked issues found for issue ${issueKey}")
    }
} else {
    logger.error("Failed to retrieve issue details for ${issueKey}: ${issueResponse.status}")
}

0 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.
September 25, 2024

Hi Ana,

It will depend on what action you want to copy the field value.

If you wanted to do it when an issue is created then you could use Behaviours and I can confirm we have some examples of how to use Behaviours here.

If you wanted to do it on an event such as when an issue is updated then you could use a script listener.

If it is just a one time task then we have the copy custom field values built-in script that you can use.

Finally, we also have an example script on the Script Console page called Copy Custom Field and this can be used as a reference to see the logic you would need to use to write a script to copy custom field values.

Using the example you could update the JQL that searches for the issue to update the field values on to return the linked issue to update the field on.

I hope this information helps.

Regards,

Kristian

0 votes
said kouzibry
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.
September 23, 2024

You can achieve this using Jira's automation with the following components : 
- trigger : whatever you want
- Branch -> for linked issue (choose the link you want or choose all)
                     --> If condition on smart value {{issue.project.key}} equals your destination project or skip this step if you only have linked issues in a single project
                     ---> Edit issue : pick your field and chose Copy from trigger issue

A screenshot would be clearer so there you go :

chrome_sWhlbs3QvR.png

 

For more info, check the following link : https://www.atlassian.com/software/jira/guides/automation/overview#what-is-automation

DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events