Forums

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

Script Runner Behaviours - How to hide required custom fields?

Relja Mihajlovic December 11, 2023 edited

I have a drop down with two options and I want to hide some custom required fields when one of the options is selected. The problem is that when I hide a field it stays required. So i cant complete issue creaton.

const customDateField = getFieldById("customfield_10062");
const dropDown= getFieldById("customfield_10059");

if(dropDown.getValue().value == 'option1'){
    customDateField.setRequired(false);
    customDateField.setVisible(false);
}else{
customDateField.setVisible(true);
    customDateField.setRequired(true);
}


So the natural thing for me was to set the field required to false when I hide it but then I get the error:

Uncaught Error: UIM tried to set an invalid value for "isRequired" property on a field (fieldId: "customfield_10062", fieldType: "com.atlassian.jira.plugin.system.customfieldtypes:datepicker")

Can someone help me?

2 answers

1 accepted

3 votes
Answer accepted
Murat Seven
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.
December 11, 2023

Hi @Relja Mihajlovic  and thanks for your question.

Can you try this code please,

const customDateField = getFieldById("customfield_10062");
const dropDown= getFieldById("customfield_10059");

if(dropDown.getValue().value == 'option1'){
    customDateField.setRequired(false);
    customDateField.setVisible(false);
}else{
customDateField.setVisible(true);
    customDateField.setRequired(true);
}

I tested the case you mentioned in my own environment and I see that it's working properly. You might have called the name you used in your definition incorrectly. If the issue persists, please provide further feedback.

 

Best,

Murat Seven

Relja Mihajlovic December 11, 2023 edited

Thank you for pointing that out but as i replayed to @Tansu Akdeniz i edited the code variable names to post it here and forgot to change that to dropDown.

Do you know what could be the problem since its not that?

When i remove these two lines

customDateField.setRequired(false);
customDateField.setRequired(true);

 Code works fine, fields get hidden. But when i try to setRequired it gives me the error?
Maybe its because my custom fields are pre setup as required?

Murat Seven
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.
December 11, 2023

@Relja Mihajlovic ,

Are you using the 'required' action with field configuration? I think instead of required custom fields, you should set this in the behavior script using 'setRequired.' Could you try it this way?

Relja Mihajlovic December 11, 2023 edited

Yea i was using 'required' action with field configuration, now it works, thank you @Murat Seven .  <3

But it kind of defeats the purpose?
Now I need to add every project to behaviour mapping (Script Runner) that I want this field to be required.

Is there a way to make this custom filed required (with field configuration) for some projects and for some not?

I guess that'
s where Field Configuration Schemes come into play?

Murat Seven
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.
December 11, 2023

I'm glad I could help :)

Yes @Relja Mihajlovic , configuring whether a specific custom field is required for certain projects is typically achievable using Field Configuration Schemes. These schemes determine how specific fields behave for particular projects.

 

If the issue is resolved, you can accept for this comment.

Hope this helps.

Best,

Murat

Relja Mihajlovic December 11, 2023

Thanks, accepted ;)

Like • Murat Seven likes this
1 vote
Tansu Akdeniz
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 11, 2023 edited

Hi @Relja Mihajlovic 

I assume you require fields via Behaviours (not jira fields config scheme).

I can't see tipDokumenta definition in your script.

Could you please check the return value below?

tipDokumenta.getValue()

You can also use formField.setHelpText(helpText) to see the value on screen as a debugging purpose.

Please take a look at this: API Quick Reference

Relja Mihajlovic December 11, 2023 edited

Sorry forgot to change that. I edited the code to poste it here and forgot to change the name of the variable. 

So that part is good.
I will fix my post. Thank you for pointing that out to me.

Do you know what could be the problem since its not that?

When i remove these two lines

customDateField.setRequired(false);
customDateField.setRequired(true);

 Code works fine, fields get hidden. But when i try to setRequired it gives me the error?
Maybe its because my custom fields are pre setup as required?

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
TAGS
AUG Leaders

Atlassian Community Events