Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
Community Members
Community Events
Community Groups

getFieldByName() not working from script runner


Hi I have following code to set field as required and the following code works as expected:

import com.onresolve.jira.groovy.user.FormField
FormField num = getFieldByName("Change Request Number");

I would like to write a wapper class around to avoid duplicate code every where.

Created groovy file called FieldAttr.groovy in behaviourutil folder
package behaviourutil

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
@BaseScript FieldBehaviours fieldBehaviours

def showRequired(String field) {"inside showRequired...." + field);
FormField sField = getFieldByName(field)

Issue is with following code, where trying to import above groovy code
import behaviourutil.FieldAttr
def d = new FieldAttr()
d.showRequired("Change Request Number");

This gives me following error:

 Script function failed on issue: (create issue) project/issuetype:  fieldId: customfield_11122, file: <inline script>
java.lang.NullPointerException: Cannot invoke method getFieldIdByName() on null object
        at com.onresolve.jira.groovy.Behaviour$getFieldIdByName$ Source)
        at com.onresolve.jira.groovy.user.FieldBehaviours.getFieldByName(FieldBehaviours.groovy:53)
        at com.onresolve.jira.groovy.user.FieldBehaviours$getFieldByName.callCurrent(Unknown Source)
        at behaviourutil.FieldAttr.showRequired(FieldAttr.groovy:19)
        at behaviourutil.FieldAttr$ Source)

1 answer

I think the issue is that you can't use the BaseScript annotation on a utility script quite like that. 

A utility class for two lines of code seems a bit like overkill, but you might be able to workaround it by using Groovy's means of turning methods into closures.


package behaviourutil

import com.onresolve.jira.groovy.user.FormField

class FieldAttr {
    static def showRequired(String field, getFieldByName) {
        FormField sField = getFieldByName(field)

Then your behaviour script would just be:

import behaviourutil.FieldAttr
FieldAttr.showRequired("TextFieldA", this.&getFieldByName)

A bit hacky, and if your method isn't actually more involved than that, I would just reduplicate the two lines of code, personally.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Apps & Integrations

Mini New Cloud App Roundup: Business Teams

We are sharing mini new cloud roundups by use case to celebrate the Atlassian Marketplace's 10th Anniversary! This mini roundup is the fourth in a series of six. Check out others in the series based ...

48 views 0 1
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you