how to make a field required based on the value of a radio button custom field

Tomas Arguinzones Yahoo March 7, 2017
Hi...I would like to make a field required (Master Branch Info) based on the value
 of a radio button custom field (Ticket Type).
 Ticket Type has these options: None, Development, Non-development. 
Basically What I need is if the user chooses Development as the value for
 the Ticket Type field, then Master Branch Info must be required.
 I know that with Behavior (as part of Scriptrunner plugin) I could achieve this
 but I dont know exactly how. I found a similar example in the Behavior documentation
 regarding the Resolution field but I am not sure exactly how to tweak it. 
Here is the example I found:

import com.atlassian.jira.issue.resolution.Resolution

def resolutionField = getFieldById("resolution")
def fixVersionsField = getFieldById("fixVersions")

def resolution = resolutionField.getValue() as Resolution

if (resolution.name == "Fixed") {
    fixVersionsField.setRequired(true)
    fixVersionsField.setHidden(false)
}
else {
    fixVersionsField.setRequired(false)
    fixVersionsField.setHidden(true)
}

 

 

Thanks for your assistance

2 answers

1 vote
Thanos Batagiannis _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.
March 8, 2017

Hi Tomas,

So the script for that behaviour will be 

def radioButtonValue = getFieldByName("Ticket Type").getValue()
def masterBranchInfo = getFieldByName("Master Branch Info")

if (radioButtonValue == "Development") {
    masterBranchInfo.setRequired(true)
}
else {
    masterBranchInfo.setRequired(false)
}

And it should be assigned to the Ticket Type field.

regards, Thanos

Tomas Arguinzones Yahoo March 9, 2017

Hi Thanos...as always thank you for your help man...I really appreciate it.

 

For some reason the behavior is "misbehaving"  smile. When I enable the behavior then I can not inline editing Ticket Type field. And then, when I try to edit the field by clicking in the Edit button at the top left of the issue and change the value of the Ticket Type field, I do see that Master Branch Info field shows the red star if I set the value of Ticket Type field to Development (and accordingly does not show the red star if I set the value of Ticket Type field to Non-development) but then I can not Update the issue (it kinda of hangs, when I click on Update nothing happens and I actually have to click Cancel, otherwise I can not close the Edit window).

Here is the XML of the behavior:

 

<config use-validator-plugin="false" name="Master Branch Info Required" description="" guideWorkflow="null">
<field id="customfield_11701" required="null" readonly="null" hidden="null" validator="server" validator-class="" validator-script="def radioButtonValue = getFieldByName(&quot;Ticket Type&quot;).getValue()&#13;&#10;def masterBranchInfo = getFieldByName(&quot;Master Branch Info&quot;)&#13;&#10; &#13;&#10;if (radioButtonValue == &quot;Development&quot;) {&#13;&#10; masterBranchInfo.setRequired(true)&#13;&#10;}&#13;&#10;else {&#13;&#10; masterBranchInfo.setRequired(false)&#13;&#10;}" validator-method=""/>
</config>

 

and here is the script you sent me which I entered as a serverside script. The behavior is associated to Ticket Type field as you indicated.

 

def radioButtonValue = getFieldByName("Ticket Type").getValue()
def masterBranchInfo = getFieldByName("Master Branch Info")

if (radioButtonValue == "Development") {
masterBranchInfo.setRequired(true)
}
else {
masterBranchInfo.setRequired(false)
}

 

Thanos Batagiannis _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.
March 14, 2017

Hi Tomas,

So, the inline edit for the field Ticket Type (the one with the assigned behaviour) is expected to be disabled. Behaviours assigned to a field have this effect. 

Now regarding the issue you have that does not let you submit the form. Could you please open your developer tools and check what kind of message you get in your console when you try to reproduce the problem ?

regards, Thanos

Tomas Arguinzones Yahoo March 16, 2017

Hi Thanos...I am seeing the following errors:

 

Uncaught TypeError: Cannot read property 'fieldType' of undefined
at HTMLInputElement.validateRequiredField (batch.js:14415)
at HTMLInputElement.dispatch (batch.js:82)
at HTMLInputElement.h (batch.js:74)
at Object.trigger (batch.js:79)
at HTMLInputElement.<anonymous> (batch.js:95)
at Function.each (batch.js:32)
at init.each (batch.js:25)
at init.trigger (batch.js:95)
at addFieldListeners (batch.js:14415)
at Object.<anonymous> (batch.js:14416)

 

I also checked catalina.out and atlassian-jira.log but I dont see any related error in those log files

Tomas Arguinzones Yahoo March 16, 2017

Hi Thanos...below is a better capture of the error I see in the console...when I select Development as the option for the Ticket Type field, I see this in the console:

 

batch.js?agile_global_admin_condition=true&hc-enabled=true&healthcheck-resources=true&jag=true&jagu…:14415 posting runvalid + Object {undefined: "", pid: "10603", issuetype: "10001", atl_token: "BUW6-0ZS6-5RR6-UUFX|6b090edffa780e3e110e2a2e5a1bb248c9ededa6|lin", formToken: "d0d96ae171827f7c410abffe34788f7733420c94"…} customfield_11701
batch.js?agile_global_admin_condition=true&hc-enabled=true&healthcheck-resources=true&jag=true&jagu…:14416 [SR Behaviours] Adding listeners ...
batch.js?agile_global_admin_condition=true&hc-enabled=true&healthcheck-resources=true&jag=true&jagu…:14416 [SR Behaviours] field: customfield_11700
batch.js?agile_global_admin_condition=true&hc-enabled=true&healthcheck-resources=true&jag=true&jagu…:14416 [SR Behaviours] Make required: customfield_11700
batch.js?agile_global_admin_condition=true&hc-enabled=true&healthcheck-resources=true&jag=true&jagu…:14416 [SR Behaviours] Require Field: customfield_11700
batch.js?agile_global_admin_condition=true&hc-enabled=true&healthcheck-resources=true&jag=true&jagu…:14416 [SR Behaviours] Validate field: undefined
batch.js?agile_global_admin_condition=true&hc-enabled=true&healthcheck-resources=true&jag=true&jagu…:14415 Uncaught TypeError: Cannot read property 'fieldType' of undefined
at HTMLInputElement.validateRequiredField (batch.js?agile_global_admin_condition=true&hc-enabled=true&healthcheck-resources=true&jag=true&jagu…:14415)
at HTMLInputElement.dispatch (batch.js?atlassian.aui.raphael.disabled=true&locale=en-US:82)
at HTMLInputElement.h (batch.js?atlassian.aui.raphael.disabled=true&locale=en-US:74)
at Object.trigger (batch.js?atlassian.aui.raphael.disabled=true&locale=en-US:79)
at HTMLInputElement.<anonymous> (batch.js?atlassian.aui.raphael.disabled=true&locale=en-US:95)
at Function.each (batch.js?atlassian.aui.raphael.disabled=true&locale=en-US:32)
at init.each (batch.js?atlassian.aui.raphael.disabled=true&locale=en-US:25)
at init.trigger (batch.js?atlassian.aui.raphael.disabled=true&locale=en-US:95)
at addFieldListeners (batch.js?agile_global_admin_condition=true&hc-enabled=true&healthcheck-resources=true&jag=true&jagu…:14415)
at Object.<anonymous> (batch.js?agile_global_admin_condition=true&hc-enabled=true&healthcheck-resources=true&jag=true&jagu…:14416)

 

Tomas Arguinzones Yahoo March 16, 2017

Hi Thanos...one more piece of information...customfield_11700 is the Master Branch Info which is a Database Custom field (using the Database Custom Field free plugin by cPrime)...so I suppose I have to include import..... at the beginning of the script?

0 votes
Manjunatha K R March 12, 2017

Hi Tomas,

I tried to use the Thanos shared behaviour script, works perfectly fine in my workflow as below:

 

Field: Global XLS Update IMPACT

Required (Optional)

Writable (Readonly)

Shown (Hide)

 

Validation Script:

import org.ofbiz.core.entity.GenericValue
import com.atlassian.jira.issue.customfields.manager.OptionsManager

def radioButtonValue = getFieldByName("Global XLS Update IMPACT").getValue()
def masterBranchInfoL = getFieldByName("Global XLS Config Input List1")
def masterBranchInfoV = getFieldByName("Global Config Input List-1 XLS Version")
def masterBranchInfoC = getFieldByName("Global XLS Config Input List-1 Components")

def actionName = getActionName()
def actionId = getAction().getId() 

//This condition added as I need to check this behaviour during this specific transition in my workflow.
if(actionId == 41 &amp;&amp; actionName == "READY FOR REVIEW"){
    if (radioButtonValue == "YES") {
    masterBranchInfoL.setRequired(true)
    masterBranchInfoV.setRequired(true)
    masterBranchInfoC.setRequired(true)
 }
 else {
    masterBranchInfoL.setRequired(false)
    masterBranchInfoV.setRequired(false)
    masterBranchInfoC.setRequired(false)
 }
}

Conditions:

  • When:
    • For action: READY FOR REVIEW (41) (Delete)
  • Except:

 

It might help you to re-check your script based on your requirements.

  • Manju

 

Tomas Arguinzones Yahoo March 13, 2017

Hi Manju...thank you for your feedback...I have no doubt that the script should work but it seems something is going on with the Behavior plugin and/on my environment that is making it to "misbehave". What is happening is what I described before:

"When I enable the behavior then I can not inline editing Ticket Type field. And then, when I try to edit the field by clicking in the Edit button at the top left of the issue and change the value of the Ticket Type field, I do see that Master Branch Info field shows the red star if I set the value of Ticket Type field to Development (and accordingly does not show the red star if I set the value of Ticket Type field to Non-development) but then I can not Update the issue (it kinda of hangs, when I click on Update nothing happens and I actually have to click Cancel, otherwise I can not close the Edit window)."

Suggest an answer

Log in or Sign up to answer