groovy for updating user picker custom field

SaidAslan June 8, 2016

Hi. Im new in groovy scripting but now need to use it in our project and faced some problems. 

The question is this.

On a certain workflow step we need to change the value of a custom field Single user picker. 

Here, in other answers I found solution, which is close to ours and tried to realize it: 

import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.ModifiedValue


def userUtil = ComponentAccessor.getUserUtil();
CustomField cf = customFieldManager.getCustomFieldObjectByName( "Approver Person");

ApplicationUser user = userUtil.getUserByKey("admin");

List<ApplicationUser> newApprovers = [];
newApprovers.add(user);

issue.setCustomFieldValue(cf, newApprovers);

Object cfVal = issue.getCustomFieldValue(cf);
IssueChangeHolder changeHolder2 = new DefaultIssueChangeHolder();
cf.updateValue(null, issue, new ModifiedValue("", newApprovers), changeHolder2);
issue.store();

But because of using JIRA 7.1 there are some errors in it. : 

 

Use UserManager.getUserByKey(string)                  ( instead of userUtil.getUserByKey("admin") )

 

Cannot call com.atlassian.jira.issue.ModifiedValue#(V, V) with arguments 

Use the Object's Service or Manager to save values.       (instead of   issue.store(); ) 

 

Soon we will upgrade it again with If / Then / Else so I would like to understand where is my mistake and how to make it work. 

And are there any books for dummies with JIRA api and groovy examples and explaining, which you could recommend?

Thanks in advance!

1 answer

1 accepted

0 votes
Answer accepted
Mahesh S
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 8, 2016

Can you please explain with an example?

Pretty confused since you have earlier mentioned that it is a Custom field user Picker field and you were trying to add multi users into it.

SaidAslan June 8, 2016

actually i could not find any mention about the single and multi picker separation by groovy (probably due to the fact that did not find questions about single user picker) so decided to try this one to look how it works smile 

what is the difference between their script realization? 

Mahesh S
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 8, 2016

Basically you need to update the custom user picker field 'Approver Person' with the value 'admin'?

Saida Aslanova June 8, 2016

yes

Mahesh S
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 8, 2016

If you have script runner plugin, Check for the builtin workflow post function 'Set value to custom field' and set the 'admin' value to the field directly.

Saida Aslanova June 8, 2016

im using script runner and first that i have tried was this "Set custom field" buildin script

 

def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Approver Person'}
issueInputParameters.addCustomFieldValue(cf.id, 'admin')

 

but it doesnt work. 

Mahesh S
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 8, 2016

If you prefer to use own scripts, please try this.

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.user.util.DefaultUserManager
import com.atlassian.crowd.embedded.api.User
 
 
IssueManager issueManager = ComponentAccessor.getIssueManager();
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField CF = customFieldManager.getCustomFieldObjectByName("Approver Person");
issue.setCustomFieldValue(CF, 'admin');

OR please try this one.

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.user.util.DefaultUserManager
import com.atlassian.crowd.embedded.api.User
 
 
IssueManager issueManager = ComponentAccessor.getIssueManager();
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField CF = customFieldManager.getCustomFieldObjectByName("Approver Person");
User usera = userManager.getUser('admin');
issue.setCustomFieldValue(CF, usera);

Apologies that I couldn't test and provide the right one, since I'm away from my work station now.

Like David Harkins likes this
JamieA
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 8, 2016

The second example above should be correct. For a single user picker field, you need to set a "single user". If you are using JIRA 6 you might need to juggle between a directory user and an ApplicationUser.

Mahesh S
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 8, 2016

Thanks! Kindly consider the second script as @Jamie Echlin [Adaptavist] has suggested. Please let us know the results. smile 

Saida Aslanova June 8, 2016

no, it writes "Unable to resolve class IssueManager". Our JIRA is 7.1, maybe this is the reason? are there changes in this version?

image2016-6-9 8:52:32.png


Mahesh S
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 8, 2016

Please add this package in the import.

import com.atlassian.jira.issue.IssueManager
Saida Aslanova June 8, 2016

updated like this:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.user.util.DefaultUserManager
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.issue.IssueManager 
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserManager

IssueManager issueManager = ComponentAccessor.getIssueManager();
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField CF = customFieldManager.getCustomFieldObjectByName("Approver Person");
User usera = userManager.getUser('admin');
issue.setCustomFieldValue(CF, usera);

 

but now it writes that the value userManager is undeclared. tried as Mizan recommended here: https://answers.atlassian.com/questions/66562

User usera = ComponentManager.getInstance().getUserUtil().getUser('admin')

and error is "Cannot find matching method com.atlassian.jira.ComponentManager#getUserUtil() 

 

Mahesh S
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 8, 2016

the method getUser() is deprecated in JIRA in recent versions. Please try it with getUserByName().

User usera = userManager.getUserByName('admin');
SaidAslan June 8, 2016

tried this too, still the value userManager is undeclared.

Mahesh S
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 8, 2016

Or on the other hand, if you are following Mizan's approach, try it as,

componentManager = ComponentManager.getInstance()
userUtil= componentManager.getUserUtil()
usera = userUtil.getUser('ngs')

Again, if your JIRA version is 6.0 or higher, use getUserByName() instead of getUser() method as,

componentManager = ComponentManager.getInstance()
userUtil= componentManager.getUserUtil()
usera = userUtil.getUserByName('ngs')
Mahesh S
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 8, 2016

Hope, this resolves the error the value userManager is undeclared.

UserManager userManager = UserManager.getInstance()
SaidAslan June 8, 2016

no, one problem replaces another one sad  maybe something else must be included or in fast-track it works somehow differently? 

 

cannot find matching method in com.atlassian.jira.util.UserManager#getInstance()

cannot assign value of type com.atlassian.jira.user.ApplicationUser to variable of type com.atlassian.crowd.embedded.api.User

 

Mahesh S June 9, 2016

Hi Saida

This seems to be an error with the casting. The approach is right for sure. As @Jamie Echlin (Adaptavist) and @Mahesh S has mentioned, the second script in the very beginning is bound to work. The only think that has to be done is if there are any deprecated methods or errors due to classes that are not imported, they have to be fixed.

 

We have done this. Can you please post the most recent script. I will try to Debug.

adammarkham
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 9, 2016

To get the UserManager and the correct user, you need:

import com.atlassian.jira.component.ComponentAccessor

def userManager = ComponentAccessor.getUserManager()

// Use this user to set the user picker to
def user = userManager.getUserByName("admin")

First problem was com.atlassian.jira.util.UserManager#getInstance() has been removed.

Second problem was the user is ApplicationUser and not User in JIRA 7.

Arun_Thundyill_Saseendran
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 9, 2016

That was spot on @Adam Markham [Adaptavist]. I was just browsing through the API documentation to find the replacement. Thanks a lot.  @Saida Aslanova can you please try this?

SaidAslan June 9, 2016

thanks a lot!!

there are no errors, its working on the custom script post-f, which is in principle suitable for solving our problem.

but actually I cant understand why this code is not working in fast track additional actions. I added the issueInputParameters.skipScreenCheck() and tried without it, exactly the same lines work great as a custom post function, but is stubbornly ignored by FT

Parameters.skipScreenCheck()

Mahesh S
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 9, 2016

wow! Thanks @Arun Thundyill Saseendran and @Adam Markham [Adaptavist].

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events