I am looking to set the assignee of an issue based on 2 fields, where the 2nd field is a user picker field.
In plain English, here's what I'm looking to do:
Here's the code I've strung together from other posts. I'd love some help.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
String user;
def cfManager = ComponentAccessor.getCustomFieldManager();
def cf = cfManager.getCustomFieldObjectByName("FieldA");
def applepicker= ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Apple Picker")
def assignee1 = issue.getCustomFieldValue(applepicker)
def blueberrypicker= ComponentAccessor.customFieldManager.getCustomFieldObjectByName("BlueberryPicker")
def assignee2 = issue.getCustomFieldValue(blueberrypicker)
def cherrypicker= ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Cherry Picker")
def assignee3 = issue.getCustomFieldValue(cherrypicker)
switch(issue.getCustomFieldValue(cf) as String){
case "Apple": user = assignee1;break;
case "Blueberry": user = assignee2;break;
case "Cherry": user = assignee3;break;
}
issue.setAssignee(ComponentAccessor.getUserManager().getUserByKey(user))
Hi @[deleted] ,
Please try the script below, I've simplified the variables and remove the redundant variable assignment.
The last part is to actually update the current issue for the script to work.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.event.type.EventDispatchOption
def cfManager = ComponentAccessor.getCustomFieldManager();
def cf = cfManager.getCustomFieldObjectByName("FieldA");
def applepicker= cfManager.getCustomFieldObjectByName("Apple Picker")
def blueberrypicker= cfManager.getCustomFieldObjectByName("Blueberry Picker")
def cherrypicker= cfManager.getCustomFieldObjectByName("Cherry Picker")
def user
switch(issue.getCustomFieldValue(cf)){
case "Apple":
user = issue.getCustomFieldValue(applepicker);
break;
case "Blueberry":
user = issue.getCustomFieldValue(blueberrypicker);
break;
case "Cherry":
user = issue.getCustomFieldValue(cherrypicker);
break;
}
try{
issue.setAssignee(user)
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
ComponentAccessor.getIssueManager().updateIssue(currentUser, issue, EventDispatchOption.ISSUE_UPDATED, false)
}catch(Exception ex){
log.debug("The issue was not updated.")
}
Fabulous! That worked like a charm @brbojorque ! I appreciate your help!
Laurie
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello @brbojorque ,
I have an error message with issue.setAssignee(user)
Jira server version 7.13.1
Thank you in advance.
Amine.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello @Nic Brough -Adaptavist- ,
I tested your script and it's working, but I wanted to ask about what if you have only one condition so we don't need the switch.
Then how the code should be in this case.
Thank you in advance.
Regards,
Amine.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
If you only have one thing, then leave out all the switch stuff, just run the one line you need
In the code above, just
user = issue.getCustomFieldValue(applepicker);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
What about the condition where I should put it
Here is my code and it's not working with one case
Here is my code :
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
String userName;
def csProduct = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("custom field")
switch(issue.getCustomFieldValue(csProduct)){
case "value1": userName = "user1" ;break;
//only one condition
}
log.error(userName)
log.error(issue.getCustomFieldValue(csProduct));
issue.setAssignee(ComponentAccessor.getUserManager().getUserByName(userName))
Thank you,
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The "as String" inside the switch statement is either pointless or going to fail to do what you want.
If FieldA is a text field holding a string, you don't need to convert it to a string. If FieldA is not a text field, then you're going to get a representation of the object it contains, not a simple string with the name of a fruit in it.
Assuming FieldA is a select list, for example, then it contains an "option" object, so you'll want to use .getValue() to get the name of the option.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for your quick response @Nic Brough -Adaptavist-
That makes sense. I've removed the string conversion. My updated code looks like this:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
String user;
def cfManager = ComponentAccessor.getCustomFieldManager();
def cf = cfManager.getCustomFieldObjectByName("FieldA");
def applepicker= ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Apple Picker")
def assignee1 = issue.getCustomFieldValue(applepicker)
def blueberrypicker= ComponentAccessor.customFieldManager.getCustomFieldObjectByName("BlueberryPicker")
def assignee2 = issue.getCustomFieldValue(blueberrypicker)
def cherrypicker= ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Cherry Picker")
def assignee3 = issue.getCustomFieldValue(cherrypicker)
switch(issue.getCustomFieldValue(cf)){
case "Apple": user = assignee1;break;
case "Blueberry": user = assignee2;break;
case "Cherry": user = assignee3;break;
}
issue.setAssignee(ComponentAccessor.getUserManager().getUserByKey(user))
Is there anything else I need to do to this to make it work? It isn't.
THanks,
Laurie
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Is FieldA really a text field?
Also, I would simplify - the "user" object should be a jira user object, not a string, then you can just use issue.setAssignee(user) without having to cast it from the assigneeX and then look it up.
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.