Make a custom field required based on another custom field

Brian Sherman March 11, 2013

My Jira version is 5.2.6 and Behaviours plugin is version0.5.3

I have a custom field that is called Production Cut-in that has a select list of Yes or No. If the user selects Yes I need to have another custom field called ECP marked as required, if the user selects no than the ECP field should not be required. I am new to groovy scripting and am not sure if I wrote the script correctly. Here is what I came up with:

FormField pro = getFieldById("customfield_11505")
FormField ecp = getFieldById("customfield_11506")
if(pro.getValue().contains('Yes')){
 ecp.setRequired(true)
}else{
 ecp.setRequired(false)
}

Any help would be greatly appreciated.

Thanks.

8 answers

1 vote
Prashant Mali February 25, 2016

You can try this for newly added custom fields on issue screen (version 7.1)

 

import com.onresolve.jira.groovy.user.FormField
FormField test1 = getFieldByName("test")
FormField test2 = getFieldById("duedate")
String str=test1.getValue()
String str2=test2.getValue()
test2.setRequired(true)
test1.setRequired(true)
if(test1.getValue() == "1")
{
log.error("************ WELCOME in first loop ************")
test2.setRequired(true)
}
else
{
log.error("************ WELCOME in second loop ************")
test2.setRequired(false)
}

0 votes
Amit Pratap Singh June 5, 2013

Hi Vidic,

how we can run above script for a specific project in jira?

do we need to create custom event for this or we need to use post-function?

0 votes
Vidic Florjan
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 11, 2013

You can try also ...

FormField pro = getFieldByName("Name_of_f_no1_not_ID1")
FormField ecp = getFieldById("Name_of_f_no2_not_ID2")

String vpro = (String) pro.getFormValue()
String vecp = (String) ecp.getFormValue()

//to see values near fields uncomment sentences
// pro.setHelpText("vpro: "+ vpro)
//ecp.setHelpText("vecp: "+ vecp ) 

  if (vpro == "Yes") {
              ecp.setRequired(true)
  }else{
        ecp.setRequired(false)
 }

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 11, 2013

Line 2 should be getFieldByName too I think...

0 votes
Andrei [errno]
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 10, 2013

have you been able to find a solution?

0 votes
Alexandru_Iacob
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 12, 2013

You may want to consider also this solution for making a custom field required based on another custom field: http://confluence.kepler-rominfo.com/display/TR/Make+a+custom+field+required+based+on+another+custom+field, using the JJupin plugin and its Live Fields feature.

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 11, 2013

Hrm, getValue() was introduced partly to avoid this need to look up an option by ID, or to hard-code an option ID, which makes it hard transferring code from dev to prod.

If you print the value that getValue() returns, you might find it's not a list, or it doesn't contain what you think.

Add

log.warn ("value returned was: " + pro.getValue())

Then tail your atlassian-jira.log, and refresh the page.


Brian Sherman March 11, 2013

Jamie,

I added the log.warn ("value returned was: " + pro.getValue()) and it said the value returned was null. Any ideas on this?

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 12, 2013

Does getFormValue() return anything?

you should also

log.warn (pro)

to make sure the field is actually found...

Brian Sherman March 12, 2013

Yes getFormValue returns -1

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 12, 2013

-1 sounds suspicious, that's not a valid option ID. What does log.warn(pro) say.

0 votes
Christian Czaia _Decadis AG_
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 11, 2013

Hey,

I would use the option ID for the "Yeas" value e.g.:

if ((pro.getFormValue() == "12135") {

...}

Either way, you should use getFormValue() and not getValue() AFAIK.

Cheers
Christian

0 votes
Christian Czaia _Decadis AG_
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 11, 2013

Hey,

I would use the option ID for the "Yes" value e.g.:

if ((pro.getFormValue() == "12135") {

...}

Either way, you should use getFormValue() and not getValue() AFAIK.

Cheers
Christian

Suggest an answer

Log in or Sign up to answer