getFieldByName() not working from script runner Edited

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");
num.setRequired(true)

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) {
log.info("inside showRequired...." + field);
FormField sField = getFieldByName(field)
sField.setRequired(true)
}

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$2.call(Unknown 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$showRequired.call(Unknown Source)
        at Script1.run(Script1.groovy:5)

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)
        sField.setRequired(true)
    }
}

Then your behaviour script would just be:

import behaviourutil.FieldAttr
log.debug("Go!")
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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Jul 10, 2018 in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

173 views 0 4
Read article

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