Trying to show fields based on selection in another field using Behavious plugin

Justin Corcoran
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.
March 14, 2012

I've been looking at the Behaviours plugin, and it looks like it does exactly what I want - it will show additional questions based on the response on a previous question. Unfortunately, I can't get it to work (probably due to my non-existant knowledge of writing code). Here's the goal:

There's a custom field called "Data Pull Type". Options are "One Time Pull" and "Data Extract". If you select One Time Pull, you shouldn't see anything extra. If you select "Data Extract" you should be presented with additional custom fields ("Extract Name", "Input Criteria").

Using the Behaviours plugin, I've done the following:

0) Created a new behaviour

1) Enabled "Validate JIRA Requiredness"

2) Added Field "Data Pull Type"

3) Added serverside script:

public void addExtractInfo() {
    FormField ExtractType = getFieldById ("customFieldId=10312")
    FormField ExtractName = getFieldById ("customfieldId=10313")
    String pulltype = (String) ExtractType.getFormValue()
    if (pulltype == "Data Extract") { 
        ExtractName.setHidden(false)
    }
    else {
        ExtractName.setHidden(true) 
    }
}

4) Added mapping to the appropriate Issue Type and Project

And now I'm stuck. FYI, the script above is my best attempt at modifying the example script Jamie provides in his plugin description to work for my purposes. As a result, it may be way off.

8 answers

1 accepted

2 votes
Answer accepted
JamieA
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.
March 14, 2012

You probably want this:

FormField ExtractType = getFieldById ("customfield_10312")
FormField ExtractName = getFieldById ("customfield_10313")
String pulltype = (String) ExtractType.getValue()
if (pulltype == "Data Extract") { 
	ExtractName.setHidden(false)
}
else {
	ExtractName.setHidden(true) 
}

The changes are:

1. the CF ID you had is wrong.

2. Assuming you are pasting this code into the UI, you do not want to wrap it in a method. It's expecting a script, like the above.

3. If you are on a recent version you should generally use getValue() instead of getFormValue(), particularly if the field is not a plain text field. Otherwise with a select you will get the Option ID and not the string value that you expect.

Justin Corcoran
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.
March 14, 2012

Worked like a charm, thanks! I'd tried the IDs the way you show, but then changed to the "=" because that's what was in the URL when I opened the custom field (and I didn't know what else to try). I really appreciate your help.

Justin Corcoran
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.
March 14, 2012

Is it also possible to make fields required, but only if they're shown? So in this example, "Extract Name" and "Input Criteria" would be both SHOWN and REQUIRED if you select "Data Extract", but HIDDEN and obviously therefore NOT REQUIURED if you select "One Time Pull".

JamieA
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.
March 14, 2012

No problem.

You need to explicitly set both attributes. I was going to change the code so that setRequired would also ensure it was shown, but I don't think I ever did that. In recent versions you can chain calls. So a more succinct version of your code would be:

FormField ExtractType = getFieldById ("customfield_10312")
FormField ExtractName = getFieldById ("customfield_10313")
String pulltype = (String) ExtractType.getValue()
def isDataExtract = pulltype == "Data Extract"
ExtractName.setHidden(!isDataExtract).setRequired(isDataExtract)

I.e. if it's data extract, set hidden: false, set required: true. The converse if it's not data extract.

Untested!

Justin Corcoran
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.
March 14, 2012

tested it out, worked perfectly, and is clear enough that I was able to add 2 more fields to this code with no problems.

Kate March 18, 2012

Hi! I try to use this script, and it works well when creating issue, but it doesn't work when i edit issue. Why does it happen?

JamieA
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.
March 18, 2012

It should work fine. You must have modified this script, are you sure you haven't introduced any changes that would break it?

What version of jira and what version of the plugin?

Simon Ward October 16, 2012

Hi There

This looks like it will do exactly what I need. However I cannot get it to work. Do you need to have the customfield id, or can you just use the name in conjunction with getFieldByName ?

Is there anything else you need to do? I have two custom fields 'Contractual Status' (a select list) and 'Contract Date to' (a date field). I only want the 'Contract Date to' field to appear when the Contractual Status is set to Fixed Term Employee.

This is the server side script I wrote based on the example above:

FormField Status = getFieldByName ("Contractual Status")
FormField EndDate = getFieldByName ("Contract Date to")
String Contract_type = (String) Status.getValue()
if (Contract_type == "Fixed Term Employee") {
EndDate.setHidden(false)
}
else {
EndDate.setHidden(true)
}

The Contract Date to field is always appearing in create and edit modes.

Any ideas? I've never written scripts before so it may be a stupid error..

Cheers

JamieA
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.
October 16, 2012

It doesn't look obviously wrong, but depending on what version you're using maybe Contract_type is not what you think it is.

Add

log.warn (" Contract_type : ${Contract_type}")

after you get the value of it, then refresh the page, and tail your logs to see the value.

Simon Ward October 16, 2012

Thanks Jamie

I'll try and get to look at the logs but not sure where they are.

JamieA
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.
October 16, 2012

<jira.home>/logs/atlassian-jira.log, typically.

Simon Ward October 16, 2012

Aha - Success! Had to use the Customid field for the Contract date to field!

Thanks for your help

Simon Ward October 18, 2012

Arrghh!

I have now added a similar script to another pair of custom fields earlier on the same screen and it doesn't work. Are you limited to one script per screen?

FormField Category = getFieldByName ("Categorisation")
FormField Replaces = getFieldById ("customfield_10026")
String Categor = (String) Category.getValue()
log.warn (" Categor : ${Categor}")
log.warn (" Category : ${Category}")
log.warn (" Replaces : ${Replaces}")
if (Categor == "New Starter - Replacement")
{
Replaces.setHidden(false)
}
else {
Replaces.setHidden(true)
}

Not even the WARN values are showing up in the log. have I missed a trick here? is there some weird publishing of the script I have to do?

I have added it to the workflow step so I am now totally bemused.

the other script I did is still working fine on the same screen.

any help graciously welcomed..

0 votes
JamieA
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.
January 23, 2013

I think "due date" should be "duedate". At least log.warn the value of dueField, so you cna see if it's null.

Any errors in the log?

0 votes
gary qiu January 23, 2013

Asked elsewhere.

0 votes
neil1967 October 16, 2012

Hi Jamie,

Running Jira 4.4.4 with Behaviours Plugin 0.5.0

I am also having a similar issue to Justin. I am trying to make the Summary field writeable only in the Create Isse screen. Because the Behaviours Plugin doesn't allow you to manipulate the Create Issue screen I've decided to make a SummaryFlag custom field (type Numeric Field) that is defaulted to 0 but gets set to 1 after the issue is created. I am trying to use a script in the Behaviours plugin to check if SummaryFlag is set to 1, and if it is, to set Summary to Readonly, but I keep getting an error in accessing the value of SummaryFlag.

Here is the script:

FormField fieldSummary=getFieldById("summary")
FormField fieldSummaryFlag=getFieldById(getFieldChanged())   

Integer flag = (Integer) fieldSummaryFlag.getValue()

if (flag == 1) {
 fieldSummary.setReadOnly(true) 
}

It appears that the fieldSummaryFlag.getValue() call returns null even though it's not empty. Here are the two errors I see in the log:

-No option found for customfield_11210 with value 1

-Something went wrong with method run in class java.lang.NullPointerException

Is my use of getValue() correct? If not, what am I missing? Thanks!

neil1967 October 18, 2012

I believe I've resolved this, though I'm not sure exactly why my solution works. Instead of using the getValue() method, I used the getFormValue() method. This returns a string representation of the Numeric Field that my custom field for a summary flag was, so I had to convert it to integer (or of course I could've converted it to boolean).

Why I could use getValue() for a default field and had to use getFormValue() for a custom field I don't understand, just as I don't understand why getting the value of a numeric custom field returns a string. Oh well, it works.

Here is the modified code:

FormField fieldSummary=getFieldById("summary")
FormField fieldSummaryFlag=getFieldById(getFieldChanged())   

if (fieldSummaryFlag.getFormValue().toInteger() == 1) {
 fieldSummary.setReadOnly(true) 
}

0 votes
Rodrigo Esteban April 26, 2012

Hi Jamie,

I have a similar problem to Justin. I have a form with several fields and depending on the selected value of a cascating select field (first field in the form), some fields become no relevant, and so, they should be hidden and unmarked as required. This groovy works fine with standards fields but I'm trying to hide a database value field using, by the way, your marvelous plugin Behaviours and, unfortunately nothing happends. That why I wonder if your plugin does not work with no-standard fields like this or the database value class has nothing of code in the setHidden method. Please, could you give me an idea of how to fix it.

JamieA
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.
April 27, 2012

If the field doesn't confirm to the standard ones then yes, it would need to be treated differently. One of the issues with this plugin is it needs to have specific knowledge of all the various field types.

0 votes
JamieA
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.
March 14, 2012

You're welcome ;-)

0 votes
JamieA
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.
March 14, 2012

Upvoted for clearly explaining the steps that you went through and including correctly formatted code!

0 votes
Justin Corcoran
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.
March 14, 2012

haha, thanks Jamie. I upvoted yours for providing an excellent plugin, and very fast and helpful support. Not to mention all the other questions you've helped with. Thanks as always.

Suggest an answer

Log in or Sign up to answer