Hi Guys and Gals,
Absolutely pulling my hair out with this. Have scoured the bowels of the internet and kinda cobbled a script together. It is as follows....
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssueString
userName;switch(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Overtime Type").toString()){
case "TOIL": userName = "user1";break;
case "PAID": userName = "user2";break;
}issue.setAssignee(ComponentAccessor.getUserManager().getUserByName(userName))
I am attempting to do this as a post-function on the create transition.
Please please can anyone help. I am using JIRA 8.5.4
Thanks in advance and hoping for a favourable reply,
Darren
So with the correct usernames in there i still get the error and the assignee field is not populated. Any ideas?
the call should be .getUserByName("correct username")
...if that fails send me another screenshot :)
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.
Can I see the code again? :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.customFieldManager
def userManager = ComponentAccessor.userManager
def issueService = ComponentAccessor.issueService
def overtimeTypeField = customFieldManager.getCustomFieldObjectsByName("Overtime Type")[0]
def overtimeTypeValue = overtimeTypeField.getValue(issue)
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueInputParameters = issueService.newIssueInputParameters()
def user
if (overtimeTypeValue.equals("TOIL")) {
user = userManager.getUserByName("user.user1")
}
else if (overtimeTypeValue.equals("PAID")) {
user = userManager.getUserByName("user.user2")
}
issueInputParameters.setAssigneeId(user.getUsername())
def validateUpdateResult = issueService.validateUpdate(currentUser, issue.id, issueInputParameters)
if (validateUpdateResult.isValid()) {
issueService.update(currentUser, validateUpdateResult)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I have to change the usernames for security reasons as cannot copy them directly to public internet
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I understand :)
Do you really have users logging in to your Jira instance with?:
Username: user.user1 Password: *************
and not just something like:
Username: user1 Password: *************
It is telling you that it cannot find the user with username matching "user.user1" so you must have it wrong :(
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
all users have first name and surname.....
john.smith
darren.palmer
Is the code working perfectly for you?
I thought something would need to be in the brackets here.....
issueInputParameters.setAssigneeId(user.getUsername(SOMETHING HERE))
but i am only guessing!
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.
I just tried the script again and it works fine. If i put "user.user1" as the value, it fails giving me the same error as you are experiancing since I don't have a user called "user.user1" in my jira instance.
The function .getUsername() doesn't take any parameters so it should be empty, it is called on the ApplicationUser Object we have saved as the variable user.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
ok :(
I guess we are at a brick wall. I even tried my own username and still get the same error. I'm not sure what else to do.
Is it something to do with the "create" transition? as this script is only executed when a brand new issue is created.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for all the help Mathis. A true gent :) I'll let you know if i can get it working
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
What version of Jira are you running?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
8.5.4 the enterprise release (hoping you have had a brainwave?)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I have finally recreated the bug. I'm working on it :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Okay. You probably have more than one field with the same name "Overtime Type" in your instance so we are most likely getting a reference to the wrong one. Try using the specific Custom Field ID as shown below. That worked for me.
You can find this id by going to Custom Field and hovering over configure as shown below. Make sure you only change the number not the customfield_ part :)
Hope this works for you!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Mathis,
Thanks again for helping me!
Unfortunately its the same error.
import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.customFieldManager
def userManager = ComponentAccessor.userManager
def issueService = ComponentAccessor.issueService
def overtimeTypeField = customFieldManager.getCustomFieldObject("customfield_18403")
def overtimeTypeValue = overtimeTypeField.getValue(issue)
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueInputParameters = issueService.newIssueInputParameters()
def user
if (overtimeTypeValue.equals("TOIL")) {
user = userManager.getUserByName("john.smith")
}
else if (overtimeTypeValue.equals("PAID")) {
user = userManager.getUserByName("david.monk")
}
issueInputParameters.setAssigneeId(user.getUsername())
def validateUpdateResult = issueService.validateUpdate(currentUser, issue.id, issueInputParameters)
if (validateUpdateResult.isValid()) {
issueService.update(currentUser, validateUpdateResult)
}
I tried with a made up username and a valif username and get the same result.
As this script is between create and the first stage in the workflow i wondered whether this was as a result of the script thinking that "overtime type" value was empty? as its populated the same time as the username that i require? Could this be that even though i select "PAID" on the ticket it thinks nothing has been populated and can therefore NOT "invoke getusername"?
I genuinely feel we are close!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
MATHIS!!!!
I have found the issue!!!
So the overtime field is a "single select" field NOT a free text field. When i use a free text field it works perfectly!
Can you help me modify the script for it to recognise that "Overtime Type" is a "single select" drop down?
Thanks you!!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Perfect! that makes sense much more sense. I'll drop you a modified script, but I have some work i need to finish first :)
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.
Okay so it should work if you just add .toString() after .getValue() :)
Since it is a select field .getValue() returns a LazyLoadedOption which we later try to call .equals() on. LazyLoadedOption != String("TOIL" or "PAID") therefore we need to cast it to a String before we compare the two :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Darren Fairweather Let me know how it turns out! :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
HI Mathis. Sorry got pulled onto something else. Works perfectly. Thanks for helping me every step of the way!!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
hi @Mathis Hellensberg , I've a similar request, however I can't get the assignee on cloned issue, could you please help take a look at it?
Objective:
Clone an issue from ProjectA to ProjectB, upon closing the issue from ProjectA, a single select list (None, User 1, User 2, User 3) shows up on close screen; if None is selected, nothing happens; otherwise, if User 1 or User2 or User3 is selected, the assignee on the cloned/new issue (projectB) will be the selected user (from single select list)
Current Status:
Cloning is working; however assignee (on projectB) is not functional
Post Function:
Condition:
cfValues['Design QA Owner']?.value
Additional issue actions:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.event.type.EventDispatchOption
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
import com.atlassian.jira.issue.IssueInputParameters
UserManager userMgr = ComponentAccessor.getUserManager()
IssueManager issueMgr = ComponentAccessor.getIssueManager()
ApplicationUser currUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def designQAOwner = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_12818")
def selectedValue = designQAOwner.getValue(issue) as String
def user
if (selectedValue == "User 1"){
user = userMgr.getUserByName("test_user1")
}
else if (selectedValue == "User 2"){
user = userMgr.getUserByName("test_user2")
}
else if (selectedValue == "User 3"){
user = userMgr.getUserByName("test_user3")
}
issue.setAssignee(user)
Logs:
2021-04-07 15:11:01,809 DEBUG [utils.ConditionUtils]: Condition did not return a boolean, coercing to true
2021-04-07 15:11:01,809 DEBUG [utils.AbstractCloneIssue]: System fields to copy: [description, summary]
2021-04-07 15:11:01,816 DEBUG [utils.AbstractCloneIssue]: Custom fields to copy: []
2021-04-07 15:11:01,885 DEBUG [utils.AbstractCloneIssue]: Run As User hlin(hlin)
2021-04-07 15:11:01,917 ERROR [recordparser.RecordParserUtils]: commentId is null
regards,
Henry
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
From what I can understand you need the cloned issue (B) to update with the assignee you select from the close screen on issue (A).
It should be a standard issue update for issue B, however you need to tell issue B that issue A was closed and an assignee was selected.
I'm afraid I don't have the time to write the code, but my approach would be something like:
Post-Function/Script Listener for issue A with condition on the assignee close field. Use LinkedIssueManager class to get a hold of linked cloned issue B and from there it is a standard update of issue B.
Hope that helps somewhat :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for the suggestion! @Mathis Hellensberg I believe the problem is that it retrieves values from the wrong issue, so in stead of retrieving values from (issue), it should be sourceIssue, which it retrieves the values from the original issue. I changed my code to use LazyLoadedOption then switch case to perform action per case; Similar to code from Adaptavist library:
https://library.adaptavist.com/entity/update-priority-based-on-a-custom-field
def selectedValue = designQAOwner.getValue(issue) as String -->
def selectedValue = designQAOwner.getValue(sourceIssue) as String
regards,
Henry
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You can't update an issue like that. You'll need to call 'issueService.validateUpdate()' after that you can call issueService.update() to actually update the value/s.
Hope this helps otherwise let me know :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Also make sure that the scriptrunner post-function script is the last on the post-function list otherwise it wont work :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for answering so quickly Mathis. I'm a bit lost to be honest. Is it possible you could help me with my code? Thanks so much.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Something like this, but then again i dont know which type the field you wanna update is. If its a user picker field and not a text field this code needs some correction.
import com.atlassian.jira.component.ComponentAccessor
def getCustomFieldManager = ComponentAccessor.customFieldManager
def userManager = ComponentAccessor.userManager
def issueService = ComponentAccessor.issueService
def overtimeTypeField = customFieldManager.getCustomFieldObjectByName("Overtime Type")
def overtimeTypeValue = overtimeTypeField.getValue().toString()
def theFieldYouWannaSet = customFieldManager.getCustomFieldObjectByName("Name of the field")
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueInputParameters = new IssueInputParameters()
if (overtimeTypeValue.equals("TOIL")) {
issueInputParameter.addCustomFieldValue(theFieldYouWannaSet.id, "user1")
}
else if (overtimeTypeValue.equals("PAID")) {
issueInputParameter.addCustomFieldValue(theFieldYouWannaSet.id, "user2")
}
def validateUpdateResult = issueService.validateUpdate(currentUser, issue.id, issueInputParameters)
if (validateUpdateResult.isValid()) {
issueService.update(currentUser, validateUpdateResult)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank Mathis.
The field i would like to set is the assignee field.
So assignee user1 is set if "TOIL" is selected in the "Overtime Type" field and assignee user2 is set if "PAID" is seleted in the "Overtime Type" field.
The "Overtime Type" field is a text field.
With the above in mind are you referencing the assignee field within this piece of code.....
def theFieldYouWannaSet = customFieldManager.getCustomFieldObjectByName("Name of the field")
is the assignee field a custom field?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Then this should work:
import com.atlassian.jira.component.ComponentAccessor
def getCustomFieldManager = ComponentAccessor.customFieldManager
def userManager = ComponentAccessor.userManager
def issueService = ComponentAccessor.issueService
def overtimeTypeField = customFieldManager.getCustomFieldObjectByName("Overtime Type")
def overtimeTypeValue = overtimeTypeField.getValue().toString()
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueInputParameters = new IssueInputParameters()
def user
if (overtimeTypeValue.equals("TOIL")) {
user = userManager.getUserByName("user1")
}
else if (overtimeTypeValue.equals("PAID")) {
user = userManager.getUserByName("user2")
}
issueInputParameter.setAssignee(user)
def validateUpdateResult = issueService.validateUpdate(currentUser, issue.id, issueInputParameters)
if (validateUpdateResult.isValid()) {
issueService.update(currentUser, validateUpdateResult)
}
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.
My bad. just add "import com.atlassian.jira.issue.IssueInputParameters" at the top :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Also i see a mistake i made.
def getCustomFieldManager = ComponentAccessor.customFieldManager
should be replaced with
def customFieldManager = ComponentAccessor.customFieldManager
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.
Sorry, I should have just tested it. This should work :)
import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.customFieldManager
def userManager = ComponentAccessor.userManager
def issueService = ComponentAccessor.issueService
def overtimeTypeField = customFieldManager.getCustomFieldObjectsByName("Overtime Type")[0]
def overtimeTypeValue = overtimeTypeField.getValue(issue)
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueInputParameters = issueService.newIssueInputParameters()
def user
if (overtimeTypeValue.equals("TOIL")) {
user = userManager.getUserByName("user1")
}
else if (overtimeTypeValue.equals("PAID")) {
user = userManager.getUserByName("user2")
}
issueInputParameters.setAssigneeId(user.getUsername())
def validateUpdateResult = issueService.validateUpdate(currentUser, issue.id, issueInputParameters)
if (validateUpdateResult.isValid()) {
issueService.update(currentUser, validateUpdateResult)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Mathis,
No errors in the code but the script is failing.. I think its to do with line 26?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It's basically telling you that you have no user named "user.user1" or "user.user2" depending of what part of the code is executing.
the value you need for the user you want to assign can be found under "User management" like so:
EDIT: Any reason you changed ".getUserByName("user2")" to ".getUserByKey("user.user2")"?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I thought it might need the actual username rather than display name. Should i change this back to how it was?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.