Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

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

Darren Fairweather November 1, 2021

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

3 votes
Max
Contributor
November 1, 2021

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')
}

Darren Fairweather November 2, 2021

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 :)

Max
Contributor
November 3, 2021

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 # people like this
Natasja Eloff
Contributor
March 14, 2022

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?

Like Vinay likes this
Max
Contributor
March 15, 2022

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".

Natasja Eloff
Contributor
March 15, 2022

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.

Max
Contributor
March 15, 2022

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

Natasja Eloff
Contributor
March 15, 2022

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

Capture2.JPG

Like Darren Fairweather likes this
Max
Contributor
March 15, 2022

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

Natasja Eloff
Contributor
March 15, 2022

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

Like Vinay likes this
Max
Contributor
March 15, 2022

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.

Like Vinay likes this
Max
Contributor
March 15, 2022

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

Like # people like this
Natasja Eloff
Contributor
March 15, 2022

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.

Like Vinay likes this
Natasja Eloff
Contributor
March 15, 2022

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

Like # people like this
Max
Contributor
March 15, 2022

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
Natasja Eloff
Contributor
March 15, 2022

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