Forums

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

Scriptrunner post function or behaviour

Stanislav
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.
June 1, 2021

Always add user (Tom Thomas) as a watcher on an issue if assignee is (John Johnson) in a certain project, for example project ABC. 

Now i only have this code plz help

import com.atlassian.jira.component.ComponentAccessor

def userManager = ComponentAccessor.getUserManager()
def watcherManager = ComponentAccessor.getWatcherManager()
def cfManager = ComponentAccessor.getCustomFieldManager()

def cf = cfManager.getCustomFieldObject((Long) 11111)
def user = userManager.getUserByName("username")

if (issue.getCustomFiledValue(cf).equals("value")){
watcherManager.startWatching(user, issue)
}

 

3 answers

1 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
June 4, 2021

Hi @Stanislav

It would be better to use the Post-Function and add a Custom Script to it.

You can try something like this:-

import com.atlassian.jira.component.ComponentAccessor

def watcherManager = ComponentAccessor.watcherManager
def userManager = ComponentAccessor.userManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

if(loggedInUser.name == "John Johnson") {
def watchUsers = { usernames ->
usernames.each {
def user = userManager.getUserByName(it)
watcherManager.startWatching(user, issue.genericValue)
}
}
def users = ["tthomas"] // use Tom Thomas' username not full name
watchUsers(users)
}

Please note, this sample code is not 100% exact to your environment. Hence, you will need to make the required modifications.

I hope this helps to answer your question. :)

Thank you and Kind regards,
Ram

Stanislav
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.
June 4, 2021

@Ram Kumar Aravindakshan _Adaptavist_ But i don't see any assignee script in your code

Stanislav
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.
June 4, 2021

it is my biggest problem

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
June 5, 2021

Hi @Stanislav,

If you want to set the watcher based on the assignee, you can modify the if condition to something like:-

if(assignee.name=="jjohnson") {
.
.
.
}

 Below is the updated working sample code for your reference:-

import com.atlassian.jira.component.ComponentAccessor

def watcherManager = ComponentAccessor.watcherManager
def userManager = ComponentAccessor.userManager
def assignee = issue.assignee
def watcher = userManager.getUserByName("tthomas")

if(assignee.name=="jjohnson") {
watcherManager.startWatching(watcher, issue)
}

Please note, this sample code is not 100% exact to your environment. Hence, you will need to make the required modifications.

I hope this helps to answer your question. :)

Thank you and Kind Regards,

Ram 

Like # people like this
1 vote
Stanislav
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.
June 1, 2021

Or maybe this one that i find

import com.atlassian.jira.component.ComponentAccessor

def watcherManager = ComponentAccessor.getWatcherManager()
def userManager = ComponentAccessor.getUserManager() 

def user = userManager.getUserObject("userid") 
if (user) watcherManager.startWatching(user, issue.genericValue)
0 votes
Vikrant Yadav
Community Champion
June 1, 2021

Hi @Stanislav user if statement like below 

issue.get("customfield_12345").getName() = “user name”

Suggest an answer

Log in or Sign up to answer