Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to add watchers after creating an issue using groovy

Vineela Durbha
Contributor
March 7, 2019

I am creating an issue using listener from one of the workflow transitions.

Now can one help with adding watchers to this newly created issue because we will not be having the issue key to be passed.

I want to add reporter from the parent issue to be copied to newly copied issue under watchers.

1 answer

1 vote
Antoine Berry
Community Champion
March 7, 2019

Hi,

May you try this code ?

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser

def watcherManager = ComponentAccessor.getWatcherManager()
def issueManager = ComponentAccessor.getIssueManager()
def watchers = issueManager.getWatchersFor(issue.getParentObject())

for (ApplicationUser watcher:watchers){
watcherManager.startWatching(watcher, issue)
}

 Also make sure you are working on a sub-task to not get an error.

Vineela Durbha
Contributor
March 11, 2019

Actually I am trying to create a new ticket in different project from one of the transitions using Listener

So in your code issue refers to current issue rite?

Antoine Berry
Community Champion
March 11, 2019

Absolutely. "issue" is the subtask that is being created.

Do you need more help ?

Vineela Durbha
Contributor
March 11, 2019

How to get reporter value for the parent object. I am trying to copy reporter value of parent object to watchers in child.

Also this is not a sub task request. It is a new issue being created but not of the type sub task

Antoine Berry
Community Champion
March 11, 2019

Ah sorry, I thought you wanted to add all the watchers from the parent issue to the child issue.

Then this should work : 

import com.atlassian.jira.component.ComponentAccessor

def watcherManager = ComponentAccessor.getWatcherManager()

watcherManager.startWatching(issue.getParentObject().getReporter(), issue)

Vineela Durbha
Contributor
March 11, 2019

I tried with above code and I am getting below error

java.lang.NullPointerException: Cannot invoke method getReporter() on null object

 

This is because parent object is returning null. This is not a sub task being created . I am just clicking on one of the button on transition screen and new ticket in different project is being created

Antoine Berry
Community Champion
March 25, 2019

Hi, have you solved your problem ? 

If I understand correctly you need to retrieve the linked issue instead of the parent issue ?

Vineela Durbha
Contributor
March 25, 2019

Yes i am looking for the linked issue only not the parent one..but here i m not trying to create any link..just i would be creating the ticket using listener from one of the transition screen..

So i am not finding a way to add watchers here because startwatching method is expecting issue as one of the parameter n I dont know how to pass that parameter

Antoine Berry
Community Champion
March 25, 2019

you could use the linkedIssue object depending on the link type (inwards or outwards) : 

import com.atlassian.jira.issue.link.IssueLink;
import com.atlassian.jira.component.ComponentAccessor;


List<IssueLink> allOutIssueLink = ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.getId());
for (Iterator<IssueLink> outIterator = allOutIssueLink.iterator(); outIterator.hasNext();) {
IssueLink issueLink = (IssueLink) outIterator.next();
def linkedIssue = issueLink.getDestinationObject()
}

List<IssueLink> allInIssueLink = ComponentAccessor.getIssueLinkManager().getInwardLinks(issue.getId());
for (Iterator<IssueLink> outIterator = allInIssueLink.iterator(); outIterator.hasNext();) {
IssueLink issueLink = (IssueLink) outIterator.next();
def linkedIssue = issueLink.getDestinationObject()
}
Vineela Durbha
Contributor
March 25, 2019

But i am not trying to establish any link ..on clicking a transition screen ,issue in a different project gets created..i m not establishing any link

Antoine Berry
Community Champion
March 25, 2019

Well you said you are linking the two issues. In that case you can use 

watcherManager.startWatching(linkedIssue.getReporter(), issue)

If there is no link between the two issues, you cannot retrieve the "source" issue, unless you use a trick like storing the key in a custom field or something similar. I would strongly suggest to just create a link between the two issues though. 

Suggest an answer

Log in or Sign up to answer