It's not the same without you
Join the community to find out what other Atlassian users are discussing, debating and creating.
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() }
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?
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:
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
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?
This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.
Read moreHey 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...
Connect with like-minded Atlassian users at free events near you!
Find a groupConnect with like-minded Atlassian users at free events near you!
Unfortunately there are no AUG chapters near you at the moment.
Start an AUGYou're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.