Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root


1 badge earned


Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!


Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.


Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!


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

Having trouble with accessing fields with scripts

Hi everyone.  

I am writing some code to access fields but it is not working. 

First off this code is in the Initializer section of a behavior.

In the code below at line 14 I am trying to get the value of the sys "Work Item Type" field but it is returning null.  I know at this point no selection has been made but I would think it would return the default value.  

On line 20 I am trying to get access to the current issue to get a hook into the custom fields.

It is also failing.



Any help would be appreciated. 



1 answer

1 accepted

0 votes
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 11, 2023

The groovy language used throughout Scriptrunner is a Java-based language.

As such, I would highly advise you to adhere to industry-standard java conventions. There are many places you can you look that up, but here is an example:

Also, groovy doesn't require any semicolons.

I'm not familiar with the "work item type" system field. Are you sure you have that right? Normally, you can find all the system field ids from the IssueFieldConstants class and I'm not seeing any system field that matches that label.

The current issue in a behaviour is available in an pre-loaded object named "underlyingIssue". If underlyingIssue is null, then you can assume that you are in the process of creating a new one.
You should not need to load it from it key like you did in line 20.
There may be cases where you need to load up a sepearate issue to fetch some custom field stored in that issue. 
I'm not clear which you intended to do here.

But regardless of whether you meant the current issue or another issue, on line 22 I see you are attempting to change an issue object with "setCustomFieldValue". With your current code this won't do anything since it's just modifying an in-memory version of that issue. You would need additional code to persist and index that change.
This is generally a bad idea from a behaviour script. Behaviour run live while a user is interacting with the screen, possibly mutiple times within a single edit session. If you are constantly changing and saving an issue object, you'll get into trouble, especially when you start having many users.

When fetching an option value from a single select custom field, it's never a good idea to just convert to string using the "toString()" method (line 23). Instead, make sure you tell your editor what type of object you expect to be returned from the custom field and use the correct method from that class.

Maybe you can explain your business objectives and I help guide you.

In the meantime, here is a version of your script that adhere to java conventions a bit more:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueFieldConstants
import com.atlassian.jira.issue.customfields.option.Option
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

def workItemTypeFormField = getFieldById('work item type') //don't know this field id, but generally, they contain no spaces
def descFormField = getFieldById(IssueFieldConstants.DESCRIPTION)
def summaryFormField = getFieldById(IssueFieldConstants.SUMMARY)

def workItemTypeValue = workItemTypeFormField.value

def customFieldManager = ComponentAccessor.customFieldManager
def textCf = customFieldManager.getCustomFieldObject('customfield_18881')
def pickerCf = customFieldManager.getCustomFieldObject('customfield_18883')

def issueKey = 'ABC-123'
def newIssue = ComponentAccessor.issueManager.getIssueObject(issueKey)

def pickerValue = newIssue.getCustomFieldValue(pickerCf) as Option
} else {
summaryFormField.setFormValue("Issue $issueKey not found")

Peter, sorry about the non standard naming conventions and formatting.

At this point I am in the learning phase of Jira and Java.  It will take me a while to learn the terminology of this environment.  So I hope you will bear with me.

I will try and do better in the future. 

As far as business need, we have a user that would like to populate a field with different text instructions depending on what issue type is picked from the issue type dropdown which is named "Work Item Type".  Pics below...........



Yes the underlyingIssue is what I was after.    I will use it and modify my code.


Thanks for your help.


Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 11, 2023

Ah... looks like "Work Item Type" is the "Jira work management" version of "Issue Type".

I've never seen a jira environment with only Jira Work management. I've only seen Jira Software and Jira Services Management.

My guess would be that in the code, it's still called "issuetype".

You can use the IssueFieldConstants class that I included in my suggestion and try the auto-complete to see if you find something like "WORK_ITEM_TYPE".

If not, then try to use IssueFieldConstants.ISSUE_TYPE.

I did use the IssueFieldConstants (Thanks for that!) and found ISSUE_TYPE.

I will check and make sure that is what I am after.




import com.atlassian.jira.component.ComponentAccessor       ;
import com.atlassian.jira.issue.Issue                       ;
import com.atlassian.jira.issue.IssueManager                ;
import com.atlassian.jira.issue.fields.CustomField          ;
import java.lang.String                                     ;
import com.onresolve.jira.groovy.user.FieldBehaviours       ;
import groovy.transform.BaseScript                          ;
import com.atlassian.jira.issue.customfields.option.Option  ;
import com.atlassian.jira.issue.IssueFieldConstants         ;
@BaseScript FieldBehaviours fieldBehaviours                 ;

def descFormField           = getFieldById(IssueFieldConstants.DESCRIPTION)     ;
def summaryFormField   = getFieldById(IssueFieldConstants.SUMMARY)         ;
def issueTypeFormField   = getFieldById(IssueFieldConstants.ISSUE_TYPE)      ;
def issueTypeCode           = issueTypeFormField.value                          ;
// sub-task = 5
// task     = 3
    getFieldById("description").setFormValue(issueTypeCode)                     ;
    getFieldById('customfield_18801').setFormValue(issueTypeCode)               ;

Code is working!
Thanks for your help Peter!

Suggest an answer

Log in or Sign up to answer
AUG Leaders

Atlassian Community Events