Jira custom field behavior does not work with different field configurations

Chiara Siegbert August 31, 2017

Hi everyone,

i wrote a script for a custom field to show or hide it based on another custom field value. The script works perfectly fine, when i create an issue and the field configuration for the issue is directly the one the fields are used in.

If the create screen starts with a different issue type or project (so of course a different field configuration) and i change to the right project and issue type, the script does not work. 

I put the script in all field configurations to the custom field, so I don't know why Jira does not uses it.

The script I put in the description of the custom field (in every field configuration) is this:

 

<script type="text/javascript">

 

jQuery(document).ready(function($) {

JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (e, context) {

    callChangeFunction();

});

  function callChangeFunction() {

        //showHidField();

    // dropdown custom field change function

    $("#customfield_10027").change(function() {

        showHidField();

    });

}

   

function showHidField() {

    //drop down field selected value

    var dropDownFieldval =$("#customfield_10027").val();

    //test field1

    $("#customfield_10404").hide();

 

    $('label').each(function(index){

        if($(this).attr('for') == 'customfield_10404'){

           $(this).hide();

        }

    });


    if(dropDownFieldval != '10063' && dropDownFieldval != ''){

         $('label').each(function(index){

        if($(this).attr('for') == 'customfield_10404'){

           $(this).show();

        }

    });

        $("#customfield_10404").show();

        $("#customfield_10404").css("border", "2px solid red");

    }

}

callChangeFunction();

});

</script>

 

2 answers

1 accepted

1 vote
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.
September 4, 2017

Hi Chiara,

I understand what you are trying to accomplish, but I do not know why this must be done in JavaScript. Why not add server-side Groovy code for the behaviour attached to the field you want to modify?

For instance, you could write something like this. 

  1. Code attached to a Select List Field
  2. Check value of the Select List Field
  3. If it's not a certain value, hide another custom field
def selectField = getFieldById(getFieldChanged())
def selectValue = cascadingField.getValue()

def customField = getFieldById("customfield_11906") // name of custom field to hide here

if (selectValue == "Some Value") {
customField.setHidden(false)
}

There are more examples of this type of code in our documentation

https://scriptrunner.adaptavist.com/latest/jira/recipes/behaviours/select-list-other.html

Chiara Siegbert September 4, 2017

Hi Joshua,

If I put the code at the same place (description of the custom field), it does not change the final result. If I create an issue and a field configuration is loaded, where the custom field is not on the create screen, and then select the issue type, where the custom field is on the create screen, the behavior of the field does not work. It ist not even loaded (I checked it with the browser console).

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.
September 5, 2017

Chiara,

Could you please clarify?

  1. Are you using ScriptRunner? If so, which version of ScriptRunner?
  2. Why must this be done in JavaScript if you are using ScriptRunner?

Showing or hiding a custom field based on the value of another field is a very basic feature of ScriptRunner Behaviours. It should not be very hard to accomplish with just a few lines of code.

I created the following behaviour, mapped to my project and specific issue type:

Screen Shot 2017-09-05 at 8.48.29 AM.pngThen, I added a field to this behaviour. Because I want to check the values of my Select List, I added my "SelectListA" field to the behaviour.

Then I added this server-side script to the SelectListA field:

def selectField = getFieldById(getFieldChanged())
def selectValue = selectField.getValue()

def customField = getFieldByName("TextFieldA") // name of custom field to hide here

if (selectValue == "AAA") {
customField.setHidden(false)
}
else {
customField.setHidden(true)
}

It gets the value of my SelectListA field. It checks to see if the value of SelectListA matches 'AAA'. If it matches, hide my other custom field named 'TextFieldA'. If it does not match, do not hide it.

Screen Shot 2017-09-05 at 8.47.58 AM.pngThere is no code being placed in the descriptions of the custom fields.

Chiara Siegbert September 5, 2017

Hi Joshua,

i understand what you wanted to me to do now. It's really much easier this way und it works perfectly fine now. 

Thank you very much for your help!

0 votes
Chiara Siegbert September 4, 2017

If I check the browser console, the following ist shown:

Empty String  handed over to getElementById() 

I assume that the script cant find the custom field, because it is not part of the create screen, but how can I avoid this behavior? It does not help to put the fields on the create screen and hide them in the field configuration.

The script should check, if the custom field is there, when changing the issue type.

Suggest an answer

Log in or Sign up to answer