It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to copy a custom field to all linkedissues based on issue update, with script listener Edited

I have a case were i would like to copy a custom field from parent Ticket to all linked issues.I can do this via jira suite utilities, in a post function but then the issue needs to transition, I really like to do this in the current status, so the only trigger i have is issue update, and i would like to do this via a groovy script and with the Script runner Listener.

I have a start Done by Kristian Walker, and activates when the parent issue is updated, and this will Add a comment to the parent, after this i would like to copy my custom field to all linked issues. 

I have very little knowledge about developing groovy scripts so all kind of help is very appreciated, trying to learn how do develop my own scripts. 

 

Below is how i did this

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.event.type.EventDispatchOption

 

// Get a pointer to the issue
Issue issueKey = issue
// Get the current logged in user
def CurrentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() as ApplicationUser
// Get access to the Jira comment and component manager
CommentManager commentManager = ComponentAccessor.getCommentManager()

// Get the customField Escalation level and it's value
def myCustomField = customFieldManager.getCustomFieldObject("customfield_12500")


// Get the last comment entered in on the issue to a String
def comment = "This issue is: " + myCustomField.getValue(issue)

def issueManager = ComponentAccessor.getIssueManager()
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def issueLinks = issueLinkManager.getInwardLinks(issueKey.getId())
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def subOutwardElements = issueLinks.findAll { it.issueLinkType.any()}

//def subOutwardElements = outwardIssueLinks.findAll { it.issueLinkType.inward.concat("is blocked by")}

log.warn("Before for loop outwardIssueLinks " + issueLinks.size())
//log.warn("Before for loop outwardIssueLinks " + inwardIssueLinks.last())


log.warn("Before for loop myCustomField " + myCustomField )

// Check if the issue is not null
if(issueKey){
// if customField Escalation level has a value
if (myCustomField.getValue(issue) != null && myCustomField.toString() == 'Escalation level' ) {
if (subOutwardElements.size() >= 0) {
for (def i = 0; i < subOutwardElements.size(); i++ ) {
log.warn("sourceId: ${i} ")
def linkedIssue = issueManager.getIssueObject(subOutwardElements[i].sourceId)
log.warn("Issue link type with name linkedIssue: ${linkedIssue} ")
log.warn("Issue link type with name myCustomField: ${myCustomField.getValue(issue)} ")

// Create a comment on the issue
commentManager.create(issueKey, CurrentUser,comment, true)
linkedIssue.setCustomFieldValue(myCustomField, myCustomField.getValue(issue))
ComponentAccessor.getIssueManager().updateIssue(user, linkedIssue, EventDispatchOption.ISSUE_UPDATED, false)

}

}
}
}

1 answer

1 accepted

@Tomas Gustavsson  Hello Tomas, i'm trying to apply this in my org.

but it is showing some errors for me. is it working for you??

Hi, Sorry for a late answer yes this did work for me, we have now removed the comment part in this script. 

What errors do you get?

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you