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!
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.
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
what is the difference between their script realization?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Basically you need to update the custom user picker field 'Approver Person' with the value 'admin'?
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.
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.
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.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks! Kindly consider the second script as @Jamie Echlin [Adaptavist] has suggested. Please let us know the results.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Please add this package in the import.
import com.atlassian.jira.issue.IssueManager
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
the method getUser() is deprecated in JIRA in recent versions. Please try it with getUserByName().
User usera = userManager.getUserByName('admin');
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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')
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hope, this resolves the error the value userManager is undeclared.
UserManager userManager = UserManager.getInstance()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
no, one problem replaces another one 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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
wow! Thanks @Arun Thundyill Saseendran and @Adam Markham [Adaptavist].
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Spend the day sharpening your skills in Atlassian Cloud Organization Admin or Jira Administration, then take the exam onsite. Already ready? Take one - or more - of 12 different certification exams while you’re in Anaheim at Team' 25.
Learn more
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.