Forums

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

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

Vishali
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, 2013

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

1 vote
Answer accepted
Bhushan Nagaraj
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, 2013

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

Vishali
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, 2013

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

Bhushan Nagaraj
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, 2013

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

Bhushan Nagaraj
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, 2013
Vishali
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, 2013

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

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

Vishali
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, 2013

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)

Bhushan Nagaraj
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, 2013

Did you miss your import statement?

import com.atlassian.jira.component.ComponentAccessor;

Vishali
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, 2013

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

Bhushan Nagaraj
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, 2013

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
}

Vishali
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 5, 2013

Bhushan,

It worked! Thank you so much for your help.

Thanks,

Vishali

Bhushan Nagaraj
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 5, 2013

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

Cheers

Bhushan

0 votes
Vishali
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, 2013

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

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events