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
4,297,434
Community Members
 
Community Events
165
Community Groups

How to get Scriptrunner Behaviour working on Transition Screen (hosted JIRA)

Edited

Hi Community,

I have successfully created a behaviour within serverside script. Its purpose is to present or hide and additional field based on the value of another field. It works perfectly on creation but does not work on screen transition. I have scoured the internet and came across this post......

https://library.adaptavist.com/entity/limit-a-behaviour-to-a-specific-screen-in-jira

......but have no idea how to integrate this into my current script or even if its the right thing to do??

My script is as follows.... (also attached :) )

 

def field = getFieldByName("IFU Current Version")
def SiteTypeOtherField = getFieldByName("IFU Current Version - Explanation")

def value = field.getValue()

if (value == "No") {
// Show this fields
SiteTypeOtherField.setHidden(false).setRequired(true)
return
}
else if (value != "No") {
// Dont show this field
SiteTypeOtherField.setHidden(true).setRequired(false)
}

 

Any help would be greatly appreciated!Screenshot 2021-11-01 at 10.16.36.png

1 answer

Hi Darren.

 

You seem to have missed the title of the screen:

final String screenName = 'Some Screen Name'

if (fieldScreen.name == screenName) {
def field = getFieldByName(fieldName)
field.value == expectedValue ? field.clearError() : field.setError('This is not a valid value')
}

Sorry Max. The page i came across that i mentioned above does something different than i want to do but it does talk about screens

 

All i want is my code above to be run on a screen. I dont need any of the other stuff about "this is not a valid value".

 

So if i wanted to run my code above just on a screen what would the code look like?

 

Thanks for help :)

import groovy.transform.BaseScript
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.issue.fields.screen.FieldScreen

@BaseScript FieldBehaviours fieldBehaviours

final String screenName = 'Test' //your transition screen name
def field = getFieldByName("IFU Current Version")
def SiteTypeOtherField = getFieldByName("IFU Current Version - Explanation")
def value = field.getValue()


if(fieldScreen.name == screenName){

if(value != null && value == "No") {
SiteTypeOtherField.setHidden(false)
SiteTypeOtherField.setRequired(true)
}
else{
SiteTypeOtherField.setHidden(true)
SiteTypeOtherField.setRequired(false)
}
}

Like Alexander Bondarev likes this

Hello, hope it's ok for me to use this thread and not start a new one...

So I have tried this script as I require, on a transition screen, fields to be mandatory. This I can do.
The part I'm struggling with is:
There is a field Radio button Know error with option Yes and No.
If Yes is selected then the field Know error detail should be visible and mandatory.
If option is "none" and No, then the field should not show.

Should the scripting be placed within the initialise field or the Know error field?

Hi Natasja, 

I do not quite understand, you want to hide the field in which you select the value?
Usually, in practice, a field is hidden depending on the value of another field. For example: Field 1 with the value Yes, hides Field2.
In this case, the script is applied to "Field1".

I have two fields.
Known error -> with options yes and no, and this field is mandatory.

There is a field Known error description, that needs to be completed if the option "Yes" is selected in the Know error field, else the field should not show on the screen.

In this case, you need to apply the script to the "Known error" field.

Thanks, Max
I did, but it's still not working.Capture.JPG

Capture2.JPG

Like Darren Fairweather likes this

Check if you have mapped to a specific project and task type for work behavior?Снимок экрана 2022-03-15 в 15.19.54.png

Capture3.JPG

Yes it's mapped.

This is the initialiser field detail as well.
This is where I set the other fields to be mandatory 
Capture4.JPG

You don't need to write the same condition for the same transition, it's enough to write the condition once and set the mandatoryness of all fields inside.
You can also set it to be mandatory using the switch, 
Example:

Снимок экрана 2022-03-15 в 15.41.35.png


Here is a brief about the work of the initializer:

Снимок экрана 2022-03-15 в 15.44.26.png

https://scriptrunner.adaptavist.com/latest/jira/tutorials/behaviours-tutorial.html

Test the script without an initializer.

Also, please note that in the script you specify "Know error", and on the screen KnowN

Like Natasja Eloff likes this

I need the fields to only be mandatory when the status is changed from "In progress" to "Under review".
Thus on the "Under review" screen.

If I use the Switch for the fields, then it's not working as expected.
It sets the fields to be mandatory regardless or status.

You don't need to write the same condition for the same transition, it's enough to write the condition once and set the mandatoryness of all fields inside. ->How do I do this?

Capture5.JPG

Error message about current scope already contains value for fieldToRequire.

Thanks the "Known error" and "Known error description" fields are now working as expected.
Will ensure I double check spelling.

Like Max likes this

You don't need to write the same condition for the same transition, it's enough to write the condition once and set the mandatoryness of all fields inside. ->How do I do this?

Since you have it on the screen, only give a name based on the name of the field.
Example:

def investigationTeamField = getFieldByName("Investigatin Team")
def generalObservationField = getFieldByName("General observation")
def rootCauseFild = getFieldByName("Root Cause")

if (getActionName() == "Under Review") {
investigationTeamField.setRequired(true)
generalObservationField.setRequired(true)
rootCauseFild.setRequired(true)
}

Like # people like this

Thank you so much for all your help.
All working as expected.

Like Max likes this

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira Software

Upcoming changes to epic fields in company-managed projects

👋 Hi there Jira Community! A few months ago we shared with you plans around renaming epics in your company-managed projects. As part of these changes, we highlighted upcoming changes to epics on...

14,698 views 37 47
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