Auto assigning of an issue based on a custom field value.

RN November 6, 2013
I am new to groovy and trying to use the below script to assign an issue based on the custom field value. Not sure whats wrong with the below script, but its not able to get into the condition. Can someone please please help me.

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.user.util.UserUtil
 
ComponentManager componentManager = ComponentManager.getInstance()
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
IssueManager issueManager = componentManager.getIssueManager()
 
def srcField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "cfname"}
def cfwt = issue.getCustomFieldValue(srcField)
def userToReassign = issue.getAssignee()
UserUtil userUtil = componentManager.getUserUtil()
log.debug("Organization Value: $cfwt") 
	switch (cfwt) {
		case "cfvalue1": userToReassign = userUtil.getUserObject("usera")
		case "cfvalue2": userToReassign = userUtil.getUserObject("userb")
		}
    issue.setAssignee(userToReassign)
    issue.store()

4 answers

1 accepted

0 votes
Answer accepted
RambanamP
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.
November 17, 2013

you forgot to add Break statement, try with following code

CustomField srcField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "fieldnname"}

def cfwt =(Option) issue.getCustomFieldValue(srcField)

if(cfwt!=null){

def cfValue=cfwt.getValue()

log.debug "my Value is ${cfValue}."

switch (cfValue) {

case 'value1': userToReassign = ComponentManager.getInstance().getUserUtil().getUser("user_n1")
break;
case 'value US/LAC': userToReassign = ComponentManager.getInstance().getUserUtil().getUser("user_n2")
break;

case 'value EMEA/APAC': userToReassign = ComponentManager.getInstance().getUserUtil().getUser("user_n3")
break;

}

issue.setAssignee(userToReassign)

issue.store()

}

RN November 17, 2013

Thanks a lot. You made my day. Thanks once again.

0 votes
RN November 17, 2013

For some reason the above code didnt work for me.

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.crowd.embedded.api.User

 CustomField srcField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "fieldnname"}


def cfwt =(Option) issue.getCustomFieldValue(srcField)

if(cfwt!=null){

def cfValue=cfwt.getValue()

log.debug "my Value is ${cfValue}."

    switch (cfValue) {

        case 'value1': userToReassign = ComponentManager.getInstance().getUserUtil().getUser("user_n1")

        case 'value US/LAC': userToReassign = ComponentManager.getInstance().getUserUtil().getUser("user_n2")

        case 'value EMEA/APAC': userToReassign = ComponentManager.getInstance().getUserUtil().getUser("user_n3")

        }

    issue.setAssignee(userToReassign)

    issue.store()

}

When I use the above script,assignee gets changed, but not working as I expected. In the issue, custom field value is set to value1, but when the script executes, it is assigning to the person in the last line (user_n3). Any suggestions?

0 votes
RambanamP
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.
November 6, 2013

select list field will return Option object so you have to try something like this

def cfwt =(Option) issue.getCustomFieldValue(srcField)
if(cfwt!=null){
def cfValue=cfwt.getValue();

  switch (cfValue) {
        case "cfvalue1": userToReassign = userUtil.getUserObject("usera")
        case "cfvalue2": userToReassign = userUtil.getUserObject("userb")
        }
    issue.setAssignee(userToReassign)
    issue.store()
	
}


					
				
			
			
			
				
			
			
			
			
			
			
		
RN November 7, 2013

Thanks Rambanam for your help. When I use this code I am getting this error.

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script21.groovy: 16: unable to resolve class Option @ line 16, column 11. def cfwt =(Option) issue.getCustomFieldValue(srcField) ^ 1 error

RambanamP
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.
November 7, 2013

use the following import

import com.atlassian.jira.issue.customfields.option.Option;

0 votes
rambabu patina
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.
November 6, 2013

Tell me which custom field/type you are using. If you use for example component field it has a componet lead so it is possible to assign to the comp lead. If lead option not available for the custom field then how can we assign to anybody?

RN November 6, 2013

Thanks Rambabu for your response. I am using a select list. I would like to use script Post function in the workflow.

rambabu patina
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.
November 6, 2013

Feel free to mark as answer/upvote if this answer helps you

rambabu patina
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.
November 6, 2013

Which jira version you are using. Try replace ComponentAccessor instead of ComponentManager if you are using above jira 5.

RN November 7, 2013

I am using Jira 5.2 and 6. In both the instances I am getting the below error.

javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: ComponentAccessor for class:

RambanamP
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.
November 7, 2013

us this import

import com.atlassian.jira.component.ComponentAccessor;

Suggest an answer

Log in or Sign up to answer