Issue with calling class/method in a custom Scriptrunner method

Deleted user May 25, 2017

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

0 votes
Stephen Cheesley _Adaptavist_
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.
June 5, 2017

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.

Deleted user June 6, 2017

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

 

Stephen Cheesley _Adaptavist_
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.
June 7, 2017

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

0 votes
Deleted user May 25, 2017

.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events