Auto assign issue based on the value selected in the custom field.

Hello!

I have custom field called Test Phase which is a single select list. I want to assign the issue to user AA when "Prod" selected and to user BB when "Dev" selected in Test Phase.

I am new to script runner and I tried following script and failed. Can you please check and let me know the problem of this script? 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.CustomField

String userName;
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjectByName("Test Phase")
def cfTestPhase = issue.getCustomFieldValue(cf)
switch(issue.getCustomFieldValue(cfTestPhase)){
case "Prod": userName = "AA";break;
case "DEV": userName = "BB";break;

}
log.error(userName)
log.error(issue.getCustomFieldValue(cfTestPhase));
issue.setAssignee(ComponentAccessor.getUserManager().getUserByName(userName))

 

3 answers

1 accepted

1 votes
Alexey Matveev Community Champion Dec 20, 2017

Hello,

Where do you put the script?

Your script should look like this and you should assign the user name to the userName variable not the user display name

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.event.type.EventDispatchOption


String userName;
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjectByName("Test Phase")
def cfTestPhase = issue.getCustomFieldValue(cf)
switch(cfTestPhase){
case "Prod": userName = "AA";break;
case "DEV": userName = "BB";break;

}
log.error(userName)
log.error(cfTestPhase);
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
issue.setAssignee(ComponentAccessor.getUserManager().getUserByName(userName))
ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)

 

Hi @Alexey Matveev

Thank you for your reply. I tried the script and it still doesn't assign the issue to the right person but the default person. 

We use our employee Ids as our username in JIRA. So AA and BB replaced by related user ids. 

I put the script as a create post function in the workflow. 

Alexey Matveev Community Champion Dec 21, 2017

You should put the script last in the post function list. Could you provide a screenshot for the create transition with all the post-functions?

Logs are as follows, 

E2222222 is the employee number of the assignee. 

Possible solutions: setAssignee(com.atlassian.crowd.embedded.api.User), getAssignee(), setAssigneeId(java.lang.String), getAssigneeId()
groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.IssueImpl.setAssignee() is applicable for argument types: (com.atlassian.jira.user.DelegatingApplicationUser) values: [E2222222]
2017-12-22 15:03:37,355 http-bio-8081-exec-9 ERROR e1111111 903x23900x1 1gkcy9a 10.56.32.65 /secure/QuickCreateIssue.jspa [scriptrunner.jira.workflow.ScriptWorkflowFunction] Script function failed on issue: BT-12, actionId: 1, file: <inline script>

Alexey Matveev Community Champion Dec 21, 2017

I guess your Jira Version is less than 7.x.x, is not it? Try this script

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.event.type.EventDispatchOption


String userName;
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjectByName("Test Phase")
def cfTestPhase = issue.getCustomFieldValue(cf)
switch(cfTestPhase){
case "Prod": userName = "AA";break;
case "DEV": userName = "BB";break;

}
log.error(userName)
log.error(cfTestPhase);
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
issue.setAssignee(ComponentAccessor.getUserManager().getUser(userName))
ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)

Hi @Alexey Matveev

Thank you very much it works for me. 

Yes my version is v 6.4.3. 

You are aware of components and component leads yes?

Thanks @Alexey Matveev You never know right =)

BTW would love to chat with you offline sometime. Drop me an email if interested. ethan at theresponsiveproject dot com

Yes I am but it doesn't fix with my requirement. 

Hi @Alexey Matveev

The script works well I have combined it with other custom field as well. 

I have a another requirement if user change the assignee during the transition, it should overwrite the auto assignee value. 

Do you have any idea how to do that? 

Alexey Matveev Community Champion Jan 21, 2018

Hello,

I saw your question. But I could not figure it out how to set the auto assignee value with ScriptRunner behaviours. I wanted to dig deeper into the source code of Scriptrunner to see if it is possible, but unfortunately I did not have time for it. Maybe someone from Adaptivist will answer the question.

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,760 views 11 18
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot