Make a custom field required based on another custom field

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

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)
}

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

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 vote

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.


Jamie,

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

Does getFormValue() return anything?

you should also

log.warn (pro)

to make sure the field is actually found...

Yes getFormValue returns -1

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

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 vote
Andrei [errno] Community Champion Apr 10, 2013

have you been able to find a solution?

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)
 }

Line 2 should be getFieldByName too I think...

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?

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

714 views 1 18
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you