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

Can I a set a behaviour to work when a user makes changes to fields on an edit/create form?

Darren Shinkins August 17, 2017

Hi,

I'm trying to make some fields read-only/invisible on a create issue screen, depending on other values the user has selected on the form.

I can get behaviours to work when opening a form on an existing issue based on the value of a field that's already set. What I'm trying to achieve is dynamically showing fields (or setting values) based on a user's selection within that form.

As a simple example, on a Create Issue screen, say I present a user with two select fields. If and only if they pick particular options from them, I may want to prompt for more information in a third field.

How can I conditionally show them that third field on that condition without making them submit their values first?

 

N.B. I'm experimenting with the Dynamic Forms for Jira plugin at the moment, but I'd much rather do this in ScriptRunner if possible, because I'd expect it would offer much more control and functionality.

 

Thanks

2 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Joshua Yamdogo @ Adaptavist
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 17, 2017

Behaviours do work dynamically on the Create screen (and other screens in general). For example:

  • A user attempts to create an issue
  • They choose a Select List Value of "New"
  • A text field is automatically hidden based on that selection, without the need to submit the form

This is an example script of that:

def reqValF= getFieldById(getFieldChanged())
def depTxtF = getFieldById('customfield_10503')
def reqValF_val = reqValF.getValue() as String

if (reqValF_val != "New") {
depTxtF.setHidden(true)
} else {
depTxtF.setHidden(false)
}

There wouldn't be any additional effort required on your part to automatically hide the field and the user won't need to actually submit the form for it to work.

Darren Shinkins August 17, 2017

Thanks for responding Joshua!

I'm just discovering the joys of getFieldChanged at the moment; so far I've got it working on a single field.

Is there any way of getting it to track changes on more than one field at a time though?

The logic I'd like to implement is something like:

if (reqVal1_val == "Field1val1" && reqVal2_val == "Field2Val1")

I'm thinking something along the lines of putting a getFieldChanged() in each of the fields I'm tracking, passing the values to variables defined in the initializer, then calling a method defined in the initializer when either of the fields changed.

I'm probably barking up the wrong tree though as my field-level scripts aren't seeing the methods I'm defining in the initializer.

There's probably an obvious solution that I've not even thought of yet, or I'm just going wrong somewhere along the way.

Any suggestions?

0 votes
Darren Shinkins August 17, 2017

What I've done now is added the following code to EACH field I want to track:

getFieldChanged();
makeChanges();

def makeChanges() {
 def f1 = getFieldByName("Field 1").getValue();
 def f2 = getFieldByName("Field 2").getValue();
    if(f1=="Value 1" && f2=="Value 2") {
     getFieldByName("Field 3").setHidden(true);
    } else {
     getFieldByName("Field 3").setHidden(falue);   

}

 

Works a treat for the most part!

 

The only thing I'm missing now is how to get the right value if Field 1 is a cascading select. I'll keep reading up, unless Joshua Yamdogo @ Adaptavist has the answer up his sleeve :)

 

In any case, I'd say this question is answered, thanks Joshua

Joshua Yamdogo @ Adaptavist
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 17, 2017

Hi Darren,

Using getValue() on a cascading select list field returns an array. To the value of either the parent or child option of a cascading select list, you'll just need to access the right index.

def cascadingSelect = getFieldById(getFieldChanged()) 
def cascadingArray = cascadingSelect.getValue()

log.debug("The values for the Cascading Select List are: " + cascadingArray)
log.debug("The first value is: " + cascadingArray[0])
log.debug("The second value is: " + cascadingArray[1])

Returns this for me:

The values for the Cascading Select List are: [BBB, B2]
The first value is: BBB
The second value is: B2  

You might get an error in the inline editor about "cannot find matching method getAt(int)." That's OK - it will still work.

Darren Shinkins August 18, 2017

Thanks for that Joshua,

Interrogating the individual array elements works perfectly.

Quick question on that first line of your log: does that still return an array, or is it another data type? See, I'm trying to query the cascading select field as a single string (to get something like if cascadingSelectValue == "Value1;Value2"). So I thought I'd write a function that receives a String[] parameter and returns a string, which concatenates cascadingArray[0] and cascadingArray[1].

Seem to be getting a class cast error when I do that though. Is there something I'm missing here?

 

Here's the code I've got so far:

def cascadingToString(String[] fieldValue, String delimiter=";#") {
    def outputString = "";
    outputString = fieldValue[0];
    if (fieldValue[1]){
        outputString += delimiter += fieldValue[1];
    }
   
    return outputString;
}
Darren Shinkins August 18, 2017

You know what, I think we're good, actually. I'm just doing the test inline by concatenating each of the two elements at the point of the test.

I'm using:

comparisonString == (fieldValue[0] + ";#" + fieldValue[1])

and that works well enough for me for now.

Thanks Joshua Yamdogo @ Adaptavist for your help on this! 

TAGS
AUG Leaders

Atlassian Community Events