I have created a user select field called 'CC'.
In the permissions, I have added some privileges for this field 'CC'
I would like to add a user to this field using groovy script.
Could any of you please tell me, how to achieve this?
Hi Vishali,
I am not entirely certain what you are trying to do but if you are trying to set a user select custom field on an issue update event:
You can use the script runner plugin to write a listener and on issue updated, set the custom field value to the user of your choice based on conditions is any.
https://jamieechlin.atlassian.net/wiki/display/GRV/Listeners#Listeners-AddWatcher
Take a look at the above link for some information.
To add a watcher in your code:
ComponentAccessor.getWatcherManager().startWatching(user,issue);
To update custom field value
Bhushan,
Here is the scenario that I have. I have a groovy script to create sub-tasks to specific assignees. One of the sub-tasks is assigned to a user. When this user completes his task and updates this ticket, the user in CC field/watcher gets notified and should start work on another sub-task that has been created. I was able to set the assignee using the following but am not able to assign a user to the CC field (which is a user select field that works as an watcher). Can you please help me with the script where I can set the user in 'CC' field or add the user as watcher ?
user= ComponentManager.getInstance().getUserUtil().getUser('abcd')
def addSubTask(issueTypeId, subTaskName) {
def issueObject = issueFactory.getIssue()
issueObject.setProject(issue.getProject())
issueObject.setIssueTypeId(issueTypeId)
issueObject.setParentId(issue.getId())
issueObject.setSummary(subTaskName + " for: " + issue.getSummary())
issueObject.setDescription(subTaskName)
issueObject.setReporter(issue.getReporter())
switch(subTaskName) {
case "Please setup account A and account B" :
issueObject.setAssignee(user)
break;
Thank you,
Vishali
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Converted your response to a comment and updated my answer to include code to add a watcher.
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.
Is it ComponentAccessor.getWatcherManager().startWatching(user,issue); or
ComponentManager.getWatcherManager().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.
Caused by: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: ComponentAccessor for class: Script26
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:318)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:111)
... 163 more
Caused by: groovy.lang.MissingPropertyException: No such property: ComponentAccessor for class: Script26
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:50)
at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:49)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:231)
at Script26.run(Script26.groovy:26)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:315)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Did you miss your import statement?
import com.atlassian.jira.component.ComponentAccessor;
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I have this script that has no errors and have not added a watcher.
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.user.util.UserManager;
import com.atlassian.jira.issue.customfields.manager.OptionsManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.fields.config.FieldConfig;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueImpl;
import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.component.ComponentAccessor;
CustomFieldManager cfm = ComponentManager.getInstance().getCustomFieldManager();
issueManager = ComponentManager.getInstance().getIssueManager()
issueFactory = ComponentManager.getInstance().getIssueFactory()
authenticationContext = ComponentManager.getInstance().getJiraAuthenticationContext()
subTaskManager = ComponentManager.getInstance().getSubTaskManager()
watcherManager = ComponentAccessor.getWatcherManager()
MutableIssue myIssue = issue
user= ComponentManager.getInstance().getUserUtil().getUser('abcd')
CustomField cfAutoID = cfm.getCustomFieldObject("customfield_12131");
def Title= myIssue.getCustomFieldValue(cfAutoID);
def addSubTask(issueTypeId, subTaskName) {
def issueObject = issueFactory.getIssue()
issueObject.setProject(issue.getProject())
issueObject.setIssueTypeId(issueTypeId)
issueObject.setParentId(issue.getId())
issueObject.setSummary(subTaskName + " for: " + issue.getSummary())
issueObject.setDescription(subTaskName)
issueObject.setReporter(issue.getReporter())
switch(subTaskName) {
case "Please setup Faxfinder, IP phone setup and Shoretel Accounts" :
issueObject.setAssignee(xyz)
watcherManager.startWatching(user,issueObject);
break;
}
switch(Title) {
case "Receptionist" :
addSubTask("5",'Please setup outlook');
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The issueObject you are trying to add the watcher to is not yet persisted. Complete creation first and then add the watcher.
Here is some example code to create an issue
User user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(); IssueInputParameters issueToCreate = ComponentAccessor.getIssueService().newIssueInputParameters(); issueToCreate.setProjectId(project.getId()); issueToCreate.setIssueTypeId(issueType.getId()); issueToCreate.setReporterId(user.getName()); issueToCreate.setSummary(issue.getSummary()); issueToCreate.setDescription(issue.getDescription()); issueToCreate.setAssigneeId(user.getName()); issueToCreate.setSecurityLevelId(issue.getSecurityLevelId()); IssueService.CreateValidationResult validationResult = ComponentAccessor.getIssueService().validateCreate(user, issueToCreate); if(!validationResult.isValid()){ //Log error } else { IssueService.IssueResult createdIssue = ComponentAccessor.getIssueService().create(user, validationResult);
//Now get the newly created issue and add a watcher
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Bhushan,
It worked! Thank you so much for your help.
Thanks,
Vishali
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Glad I was able to help you out. Please accept the answer and vote up if it helped.
Cheers
Bhushan
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Is it ComponentAccessor.getWatcherManager().startWatching(user,issue);
or ComponentManager.getWatcherManager().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.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.