Issue with calling class/method in a custom Scriptrunner method Edited

Hello when writing a custom setGalaxy method like below

 

1  import com.atlassian.jira.issue.ModifiedValue
2  import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
3  import com.atlassian.jira.issue.MutableIssue
4  import com.atlassian.jira.issue.CustomFieldManager
5  import com.atlassian.jira.issue.Issue
6  import com.atlassian.jira.component.ComponentAccessor
7  import com.atlassian.jira.issue.label.LabelManager
8
9  MutableIssue issue = (MutableIssue) event.issue;
10
11 CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
12 def groupManager = ComponentAccessor.getGroupManager()
13
14 def setCF(String updatedValue,String issueSet) {
15    def thisCF = customFieldManager.getCustomFieldObjects(issueSet).find {it.name == 'Some Field'}
16    def fieldConfig = thisCF.getRelevantConfig(issueSet)
17    def setValue = ComponentAccessor.optionsManager.getOptions(fieldConfig).find { it.value == updatedValue }
18    def changeHolder = new DefaultIssueChangeHolder()
19    thisCF.updateValue(null, issueSet, new ModifiedValue(issueSet.getCustomFieldValue(thisCF), [setValue]),changeHolder)
20 }
21
21 setCF("Potato",issue)

I get the following errors in the method:

Line 15 The variable [customFieldManager] is undeclared and No such property: name for class: java.lang.Object

Line 16 - 19 get other errors due to thisCF not being set. 

Moving customFieldManager into the method doesn't help.

There is a similar issue here https://community.atlassian.com/t5/Atlassian-Marketplace-questions/Basic-method-to-update-a-custom-f... but everything is outside the method.

 

JIRA Software 7.2.4 Scriptrunner 5.0.4

2 answers

This widget could not be displayed.

.

This widget could not be displayed.

Hi John,

The groovy documentation here in section 3.4 explains why you are having the issue your are. Script variables do not require a type definition.

If you define the type (def, CustomFieldManager, e.t.c.) it will define the variable as local and will NOT be available outside the main script declaration (including inside internal methods).

You can fix the problem by taking the type definition away:

cFManager = ComponentAccessor.getCustomFieldManager()

def galaxySet(galaxyName,issueName) {
def galaxyCf = cFManager.getCustomFieldObjects(issueName).find {it.name == 'Galaxy'}

You'll notice that all we need to do is take away the type definition and we get access inside our method.

I get "The variable [cFManager] is undeclared." when I remove the type

 

Hi John,

I've looked at the linked community post you provided. I've noticed several other issues in the example that you provided. I also can't find the original setGalaxy method in the example that you gave.

Perhaps it would be easier if I understood what you are trying to accomplish with this script. This would make it easier to work around the issues that you are seeing.

Thanks,

Steve

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted yesterday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

71 views 1 0
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you