Hello,
I have been reading a lot of community Q&A about using Scriptrunner to add a watcher on create based on a custom field. I put this together, added it after Creates the issue originally and after Re-Index and issue. It is not giving me an error, and there have been no failures on executions, but it is also not adding the watcher. Anyone care to opine? In case it matters, jira server v8.3.4 and Scriptrunner v5.6.2
import com.atlassian.jira.component.ComponentAccessor
def userManager = ComponentAccessor.getUserManager()
def watcherManager = ComponentAccessor.getWatcherManager()
def cfManager = ComponentAccessor.getCustomFieldManager()
def cf = cfManager.getCustomFieldObject("customfield_xxxxx")
def user = userManager.getUserByKey("uname")
def cfFieldValue = issue.getCustomFieldValue(cf)
if (cfFieldValue == 'Value'){
watcherManager.startWatching(user, issue)
}
you can try placing this function in the bottom of the order, also attaching my snippet for reference
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
def watcherManager = ComponentAccessor.getWatcherManager()
def userManager = ComponentAccessor.getUserManager()
if(issue.getSummary() == "Watcher Adding"){
ApplicationUser user = userManager.getUserByName("UsernName")
watcherManager.startWatching(user, issue)
}
(I tried this snippet and it does works for me)
BR,
Leo
Tried it at the bottom and it did not work.
Is your code looking for the Summary? How do I define or get the value of a custom field using your suggestion?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Here you go for Custom Field value
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField= customFieldManager.getCustomFieldObjectByName("Custom Field Name")
def value = issue.getCustomFieldValue(cField)
if(value == "Something"){
//Do something....
}
In your script you missed "ById" in getCustomfield method so the code is
def cf = cfManager.getCustomFieldObjectById("customfield_xxxxx")
BR,
Leo
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
So putting your suggestions together would look like this? It still did not work. Also, please note some comments in the script.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
def watcherManager = ComponentAccessor.getWatcherManager()
def userManager = ComponentAccessor.getUserManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField= customFieldManager.getCustomFieldObjectByName("Custom Field Name") //this line is giving me a warning to use customFieldManager.getCustomFieldObjectsByName instead
def value = issue.getCustomFieldValue(cField) //if I switch it to what it suggests above it says it cannot find this method.
if(value == "Something"){
ApplicationUser user = userManager.getUserByName("uname")
watcherManager.startWatching(user, issue)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
You can ignore the warning message, or you can go with getCustomFieldById("customfield_xxxxx")
logger method and find out what you are getting the value in your script or try to log something on conditional loops(I don't find any errors)
Where you are placing this function. in the last of the order?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Leo Thanks for all your help so far. I put it in the last step, after firing the event. Also, would the evaluation part need to be written differently if the field is a single select?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
No, it's same like other fields. you can use the same along with as String >>>> issue.getCustomFieldValue(cField) as String
to get the value and validate it
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That did it, I forgot the "as String" piece. Once I added that, it worked. Thanks @Leo !
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.