Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

scriptrunner listener issue with the post function snippets

Stéphane Blondeau September 3, 2020

Hi,

I've created a built-in listener "Fast-track transition an issue" with scriptrunner plugin from Adaptavist

The event is fired properly, the transition is executed and some post-functions are also executed.

One of this post function is to update a custom field (a select-list one).

I've copied the "Set select list custom field" snippet" which is :

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customField = customFieldManager.getCustomFieldObjectByName("Some Select List") // name of CF
def optionsManager = ComponentAccessor.getOptionsManager()
def fieldConfig = customField.getRelevantConfig(issue)
def option = optionsManager.getOptions(fieldConfig).find { it.value == "Yes" } // value of option

issueInputParameters.addCustomFieldValue(customField.id, option.optionId as String)
issueInputParameters.setSkipScreenCheck(true)

 

The code analysis sets a warning on the customFieldManager definition, saying "Variable "customFieldManager" masks a binding variable. Please choose a different name for variable: "customFieldManager" @ line 8, column 5.

 

I've several listeners configured the same way (but with different custom fields...all select-list). Some works, some don't...

I'm new with scriptrunner so I may have missed something...but I would exect built-in snippets to work from scratch....

If someone from Adaptavist could help, this would be great !

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Gustavo Félix
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.
September 3, 2020

Hi @Stéphane Blondeau 
Just to be sure if I understand.
You have a customField that is a select-list. 
And this field have X options and on your code you want to select the option with "yes" value. 
Am I right?

Stéphane Blondeau September 3, 2020

Hi Gustavo.

yes this is exactly the case.

To be precise, I’ve configured several listeners with different field names and options values...but if it works with this value...I’m sure I will make it work with others...

Gustavo Félix
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.
September 3, 2020

 

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customField = customFieldManager.getCustomFieldObjectByName("Some Select List") 

def fieldConfig = customField.getRelevantConfig(issue)

def value = ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find{it.toString()=='yes'}

issue.setCustomFieldValue(customField,value)
issueManager.updateIssue(user,issue,EventDispatchOption.DO_NOT_DISPATCH,false)

This is working for me, I hope it works for you aswell.

Stéphane Blondeau September 3, 2020

Thanks.

Could you give me the complete script (with import declarations) ?

By the way, the problem remains regarding this line :

def customFieldManager = ComponentAccessor.getCustomFieldManager()

I still have the same warning preventing the code to be executed :

Variable "customFieldManager" masks a binding variable. Please choose a different name for variable: "customFieldManager" @ line 8, column 5

Gustavo Félix
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.
September 3, 2020

Sure, I forgot the imports.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.event.type.EventDispatchOption

def issue = issue as MutableIssue
def userManager = ComponentAccessor.getComponent(UserManager)
UserUtil userUtil = ComponentAccessor.getUserUtil()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
IssueManager issueManager = ComponentAccessor.getIssueManager()
def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)
def selectField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("select")
def customFieldM = ComponentAccessor.getCustomFieldManager()
def fieldConfig = selectField.getRelevantConfig(issue)
def value = ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find{ it.toString() == 'yes' }
issue.setCustomFieldValue(selectField,value)
issueManager.updateIssue(user,issue,EventDispatchOption.DO_NOT_DISPATCH,false)


This is my complete script with the variables I used.



Stéphane Blondeau September 3, 2020

Thanks again.

I've simply copied your script in the post-function tab of the listener and I have the same issue with this line :

def issue = issue as MutableIssue

Not sure, but it seems "issue" is already defined when using the built-in listener option (fast track transition).

Do you use this script with a fast track transition listener ? or in an other context ?

Gustavo Félix
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.
September 4, 2020

I'm using it in a Post Function, when the issue is created.

I think the equivalent in a listener would be 
def issue = event.issue or MutableIssue issue = event.issue as MutableIssue


Stéphane Blondeau September 4, 2020

Well thanks again...

but this doesn't help regarding this error :

Variable "customFieldManager" masks a binding variable. Please choose a different name for variable: "customFieldManager" @ line 8, column 5

I've tried to rename the variable, but some other errors appear...

Gustavo Félix
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.
September 4, 2020

Sorry, I'm not sure what the error could be without seeing the entire script.
I just noticed that I didnt need that line,
In my script, 
def customFieldM = ComponentAccessor.getCustomFieldManager()
"customFieldM" is not used.
Could you remove it to see if another error appears?

DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events