Forums

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

updating the watcher field using scriptrunner

sahere
Contributor
August 27, 2019

hi all

in the create issue screen there is a field as Watchers, I want to update this field even if the user didn't do it in the creating step. I used following script in scriptrunner post function so that add "b-atb" user to Watcher list :

 

import com.atlassian.jira.issue.MutableIssue

import com.atlassian.jira.component.ComponentAccessor;

import com.atlassian.jira.ComponentManager;

import com.atlassian.jira.issue.CustomFieldManager;

import com.atlassian.jira.issue.fields.CustomField;

import com.atlassian.jira.issue.IssueManager;

import com.atlassian.jira.issue.Issue

import com.atlassian.jira.issue.CustomFieldManager

import com.atlassian.jira.issue.MutableIssue

def customFieldManager = ComponentAccessor.getCustomFieldManager()

 

MutableIssue issue = issue as MutableIssue

 

def myField = customFieldManager.getCustomFieldObjectByName("Watchers")

issue.setCustomFieldValue(myField,'b-atb')

 

 

but after publishing, when i tried to create an issue, following message has been shown in the create screen:

We can't create this issue for you right now, it could be due to unsupported content you've entered into one or more of the issue fields. If this situation persists, contact your administrator as they'll be able to access more specific information in the log file.

any advise will be so appreciated

2 answers

1 accepted

0 votes
Answer accepted
Andrew
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.
August 27, 2019

Hi @sahere ,

I suppose You need use startWatching(ApplicationUser user, Issue issue)

Also not sure setCustomFieldValue(myField,'b-atb') it not work because You try set String to Watch field.

To find user You can use DefaultUserManager.

More info

https://docs.atlassian.com/software/jira/docs/api/7.1.1/com/atlassian/jira/issue/watchers/WatcherManager.html

https://docs.atlassian.com/software/jira/docs/api/7.1.1/com/atlassian/jira/user/util/DefaultUserManager.html

B.R.

sahere
Contributor
September 3, 2019

many thanks

0 votes
Tarun Sapra
Community Champion
August 27, 2019

Hello @sahere 

You are getting the error, because you are treating "watcher" as a custom field and thus gettting error in your logs.

Please use watcher Manager as defined in API

https://docs.atlassian.com/software/jira/docs/api/7.6.1/com/atlassian/jira/issue/watchers/WatcherManager.html

Example code here, please see answer from Henning Tietgens

https://community.atlassian.com/t5/Answers-Developer-Questions/How-do-I-add-a-specific-user-as-a-watcher-using-script-runner/qaq-p/569185

Tarun Sapra
Community Champion
August 27, 2019

image.png

sahere
Contributor
September 3, 2019

many thanks 

Suggest an answer

Log in or Sign up to answer