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

Aravindi Amarasinghe December 20, 2017

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
Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 20, 2017

Hello,

Where do you put the script?

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 20, 2017

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)

 

Aravindi Amarasinghe December 21, 2017

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
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 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?

Aravindi Amarasinghe December 21, 2017

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>

Aravindi Amarasinghe December 21, 2017

image.png

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 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)
Aravindi Amarasinghe January 2, 2018

Hi @Alexey Matveev

Thank you very much it works for me. 

Yes my version is v 6.4.3. 

Saurabh Patel June 23, 2022

Hi @Alexey Matveev 

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

2 votes
Ethan Foulkes December 22, 2017

You are aware of components and component leads yes?

Ethan Foulkes December 22, 2017

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

Aravindi Amarasinghe January 21, 2018

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

0 votes
Aravindi Amarasinghe January 21, 2018

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
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 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 Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events