When reporter is [x], set single list single choice custom field to [y]

Eric Smith March 7, 2019

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?

2 answers

0 votes
fjodors
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.
March 17, 2019

I defined these variables directly in my code (I use script listeners and rest endpoints)

Eric Smith March 18, 2019

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?

fjodors
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.
March 18, 2019

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

0 votes
fjodors
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.
March 7, 2019

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.

Eric Smith March 8, 2019

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
{
}
fjodors
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.
March 10, 2019

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;
....
Eric Smith March 15, 2019

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.

Suggest an answer

Log in or Sign up to answer