Copy value from user picker custom field to Watchers filed on custom field update Edited

We have a request to copy values from two user picker fields to the Watcher filed whenever these fleds are updated. I have created a script listener which runs on issue update:

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor

def issue = event.issue as Issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def sourceField = customFieldManager.getCustomFieldObjects(event.issue).find {it.name == "Technical Lead"}
def tgtField = customFieldManager.getCustomFieldObjects(event.issue).find {it.name == "Watchers"}
def changeHolder = new DefaultIssueChangeHolder()
tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), issue.getCustomFieldValue(sourceField)),changeHolder)

 

But this does not update the watcher filed. We get the following error:

 

2017-12-13 10:21:39,209 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2017-12-13 10:21:39,209 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: <inline script>
java.lang.NullPointerException
	at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:896)
	at com.atlassian.jira.issue.Issue$getCustomFieldValue$9.call(Unknown Source)
	at Script48.run(Script48.groovy:11

I guess that the method for get and set Watchers filed is not correct. Can you please help us with that?

 

Regards,

Nadica

1 answer

0 vote

Hi Nadica,

Indeed, the way you are trying to set the value for 'Watchers' field seems to be the problem here.

Assuming 'issue.getCustomFieldValue(sourceField)' returns an 'Application User' object, try this:

def watcherManager = ComponentAccessor.getWatcherManager()

watcherManager.startWatching(issue.getCustomFieldValue(sourceField), issue)

 

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Asked 12m ago in Confluence

What are the resources that you use to learn more about Atlassian Products?

I am gathering information about resources available for Atlassian product knowledge transferring for a presentation in our local Atlassian User Group. I want to group them in four categories From ...

3 views 0 1
View question

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you