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

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

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()

}

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

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?

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

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

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

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:

us this import

import com.atlassian.jira.component.ComponentAccessor;

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()
	
}


					
				
			
			
			
				

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

use the following import

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

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?

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

161 views 1 17
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