How do I set a custom field value (user select field, not watcher) to a single user using Groovy script?

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?

2 answers

1 accepted

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

Converted your response to a comment and updated my answer to include code to add a watcher.

Is it ComponentAccessor.getWatcherManager().startWatching(user,issue); or

ComponentManager.getWatcherManager().startWatching(user,issue);???

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)

Did you miss your import statement?

import com.atlassian.jira.component.ComponentAccessor;

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');
}

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
}

Bhushan,

It worked! Thank you so much for your help.

Thanks,

Vishali

Glad I was able to help you out. Please accept the answer and vote up if it helped.

Cheers

Bhushan

Is it ComponentAccessor.getWatcherManager().startWatching(user,issue);

or ComponentManager.getWatcherManager().startWatching(user,issue);??

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

347 views 0 6
Read article

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