Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,298,656
Community Members
 
Community Events
165
Community Groups

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 vote
Answer accepted

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 _Appfire_

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. 

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>

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 _Appfire_

Thank you very much it works for me. 

Yes my version is v 6.4.3. 

Hi @Alexey Matveev _Appfire_ 

This script works like a charm. You saved my time today!! 

You are aware of components and component leads yes?

Thanks @Alexey Matveev _Appfire_ 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 _Appfire_

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? 

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 Sign up to answer
TAGS

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you