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()
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()
}
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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()
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
use the following import
import com.atlassian.jira.issue.customfields.option.Option;
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Feel free to mark as answer/upvote if this answer helps you
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Which jira version you are using. Try replace ComponentAccessor instead of ComponentManager if you are using above jira 5.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
us this import
import com.atlassian.jira.component.ComponentAccessor;
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.