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

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
Community showcase
Posted Sep 18, 2018 in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

23,106 views 2 7
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