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

This widget could not be displayed.

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

This widget could not be displayed.

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

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

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Aug 22, 2018 in Marketplace Apps

How a Marketplace app tech team is achieving gender diversity

Hello! My name is Genevieve Blanch, and I'm the Marketing Manager at RefinedWiki, creators of apps to give teams the tools to customize Atlassian platforms. Currently, 44% of the tech team at Re...

428 views 3 17
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