Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.
×Im trying to change a custom field called "ITOPS Team" to "Windows" when a ticket is created with the reporter "Ticketshuttle_ITOPS". Here is the code I have so far:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
def reporter = issue.getReporter()
def customFieldManager = ComponentAccessor.customFieldManager
def escalate = customFieldManager.getCustomFieldObjectsByName("ITOPS Team").first()
def optionsManager = ComponentAccessor.getOptionsManager()
def config = escalate.getRelevantConfig(issue)
def options = optionsManager.getOptions(config)
def optionToSelect = options.find {
it.value == "Windows"
}
if ((reporter)== "ticketshuttle_ITOPS"){
escalate.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(escalate), optionToSelect), new DefaultIssueChangeHolder())
}
else
{
}
However, when I run the code, the error in the log is:
2019-03-07 16:02:00,697 http-nio-8080-exec-7 url:/secure/QuickCreateIssue.jspa username:ticketshuttle_ITOPS ERROR ticketshuttle_ITOPS 962x113212x1 pcmkgx 173.31.112.42,10.231.220.22,10.231.13.23 /secure/QuickCreateIssue.jspa [c.o.s.jira.workflow.ScriptWorkflowFunction] *************************************************************************************
2019-03-07 16:02:00,697 http-nio-8080-exec-7 url:/secure/QuickCreateIssue.jspa username:ticketshuttle_ITOPS ERROR ticketshuttle_ITOPS 962x113212x1 pcmkgx 173.31.112.42,10.231.220.22,10.231.13.23 /secure/QuickCreateIssue.jspa [c.o.s.jira.workflow.ScriptWorkflowFunction] Script function failed on issue: null, actionId: 1, file: <inline script>
java.util.NoSuchElementException: Cannot access first() element from an empty List
at Script182.run(Script182.groovy:15)
Anyone know why it thinks the element is empty?
I defined these variables directly in my code (I use script listeners and rest endpoints)
I think part of the confusion here is that you linked me to a solution, but the solution is another link to more code. I think I see what you are trying to get at but your recycled answer appears to be for a listener and I am trying to configure a post-function in the workflow. Furthermore, I see in the second where you define issueToUpdate as:
MutableIssue issueToUpdate = (MutableIssue) event.issue;
But im still not seeing anywhere in your solution where you defined ComponentManager. the only reference I see is on the first link, where
def optionsManager = ComponentManager.getComponentInstanceOfType(OptionsManager.class)
This defines optionsManager, but not ComponentManager. Is if possible that its able to be used like that in the listener but not in a custom script in a post-function?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi
Considering ComponentManager - see my answer from Mar 10, 2019
import com.atlassian.jira.ComponentManager
def componentManager = ComponentManager.getInstance()
Then you will be able to operate with componentManager.
I have not tried it in postfunctions, but I think it should be similar logic like in script listeners
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi
Check this article https://community.atlassian.com/t5/Adaptavist-questions/Listener-Clone-ticket-is-not-updating-a-CF-after-creation/qaq-p/918043 , my reply from Oct 29, 2018
There was an example how to update select list custom field.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I put in the above mentioned code into my script but ComponentManager and issueToUpdate variables are undefined. I noticed that you have issueToUpdate commented out at the top, but if thats referring to defining an issue number it should be noted that im trying to run this as a postfunction in a workflow:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
def reporter = issue.getReporter()
//issueToUpdate - your target issue
def cf =customFieldManager.getCustomFieldObjectByName('ITOPS Team');
def fieldConfig = cf.getRelevantConfig(issueToUpdate)
def optionsManager = ComponentManager.getComponentInstanceOfType(OptionsManager.class)
def optionA = optionsManager.getOptions(fieldConfig).find {it.value == "Windows"}
issueToUpdate.setCustomFieldValue(cf, optionA);
issueManager.updateIssue(event.getUser(), issueToUpdate, EventDispatchOption.ISSUE_UPDATED, false);
if ((reporter)== "ticketshuttle_ITOPS"){
cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), optionA), new DefaultIssueChangeHolder())
}
else
{
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi
if these variables are undefined, you should specify them
For example, how it works in script listener (in postfunction it should be similar):
....
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.MutableIssue;
def componentManager = ComponentManager.getInstance()
/*get issue - you need MutableIssue if you would like to update this issue*/
MutableIssue issueToUpdate = (MutableIssue) event.issue;
....
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
They appear to be variables in your example that is either pre-defined in whatever interface you are using (Behavior? Script Listener?), or you have defined these variables somewhere else that I am unaware of. I did not create these variables so im unsure how they need to be defined to get your linked example to work.
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.