Forums

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

Show/Hide a field on the create screen with scriptrunner behaviour

Nguyen Tran
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 5, 2018

I have 2 custom fields named "Contract type" and "Legal Reviewer" where "Contract type" is a single choice select list (2 options: Template, Non-template) and "Legal Reviewer" is a single user picker.

Currently "Legal Reviewer" is set as hidden. I want to show it when user choose "Non-template" for "Contract type" on the create screen.

Here's my attempt:

def fieldToHide = getFieldByName("Legal Reviewer")
def contractCf = getFieldByName("Contract type")
def optionValue = contractCf.getValue().toString()


fieldToHide.setHidden(true)

if (optionValue == 'Non-template') {
fieldToHide.setHidden(false)
}

There are no errors shown but the script doesn't work. When I choose "Non-template" the field "Legal Reviewer" doesn't show up. Any idea what I did wrong here? Thanks in advance. 

2 answers

1 accepted

5 votes
Answer accepted
Alexey Matveev
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 5, 2018

You need to add the Contract type field to your behaviour and define the script in your question for the added field. Right now I guess you defined the script for the whole behaviour.

Nguyen Tran
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 6, 2018

You're right. I just need to move the script to the behaviour for Contract Type field. Thanks!

Alexey Matveev
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 6, 2018

Initializer fires only once, when a screen is loaded. Server Side script for a field fires everytime, when the value of the field changed.

Nguyen Tran
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 6, 2018

Got it! Thanks for the clear response.

Deleted user March 27, 2018

I also have the same requirement. I tried the same as explained above but its not working. Here I can use the normal Text Fields too right? or any special Type of Field? 

And should the fields be a part of the Request ( Customer Portal) or should not be included there, if we want to hide them depending on the other field behaviour ? How does Field Change work here?

Do I also need a Initilaiser ?

@Nguyen Tran what do you mean by " I just need to move the script to the behaviour for Contract Type field" When I add the field in the Behaviour then I can just add the Server Side Schript below it. This is what needs to be done. Right? or there is any other setting which I might be missing ?

 

Thanks in advance.

Nguyen Tran
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 27, 2018

Hi @[deleted],

You should be able to do it with text field as well. What I did was putting the server side script into the "Contract type" field (It was in the "Legal Reviewer" field).

Let say you have field A with a few options and field B that will be showing/hidden depends on field A. Put your server side script to field A so the script fire every time field A's value changes.

Hope this helps.

Nguyen

Jon Schewe July 27, 2018

Does that mean that field A needs to be a scriptable field? Or is there a way to attach the script to a regular field?

Jon Schewe July 27, 2018

I got it figured out, behaviors is the right answer for me.

Deleted user December 4, 2018

Hi guys,

I have taken Nguyen Tran code, and put it into my behaviour, in the Urgencia field, server-side.

def fieldToHide = getFieldByName("Oculto")
def urgencia = getFieldByName("Urgencia")
def optionValue = urgencia.getValue().toString()

fieldToHide.setHidden(true)

if (optionValue == 'Critical') {
 fieldToHide.setHidden(false)
}

and there is no error, but it seems it does not do anything, I mean, I log into the customer portal, raise a request, and in the create screen I see Oculto field, though the Urgencia value is not Critical.

I appreciate any help, please. I am new in this kind of things. Thanks in advance.

Cheers,

Almu

0 votes
Gezim Shehu [Communardo]
Community Champion
March 5, 2018

I think you need to specify the issue when getting a custom field value.

Something like:

customField.getValue(issue)

Nguyen Tran
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 5, 2018

I want to do this when user is creating the issue. So on the Create screen user would select option for the custom field and the hidden field will show up or hidden according to the selected option. Would the value for "issue" be there if the issue is not created?

Gezim Shehu [Communardo]
Community Champion
March 5, 2018

Where are you running this script?

Is it as a post function? If it is then it will only execute once the transition occurres.

 

Maybe you could write a validation script, to only accept the value based on the first field's value?

Gezim Shehu [Communardo]
Community Champion
March 5, 2018

See below comment from Alexey.

 

Missunderstood the question. You need to configure behaviors.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events