How do you set a custom field as required based on values of other custom fields in the same screen

Hi, Newbie here.  I have a basic workflow with custom fields.  I would like to set some fields required based off values that the user enters in the same screen.  I have script runner, but need help with how to code it. Any examples? 

1 answer

Hi Nagata,

                 You can add a behaviour.

import com.atlassian.jira.component.ComponentAccessor
def jpField = getFieldByName("JIRA Project")
def urlField = getFieldByName("GitLab URL")
def aurlField = getFieldByName("Additional URL's / Notes")
def NPField =getFieldByName("Name of Project")
def PKField = getFieldByName("Project Key")
//def JPFIeld = getFieldByName("JIRA Project")
def fvField = getFieldById("fixVersions")
def sField = getFieldByName("Sprint")
def tField = getFieldById("timetracking_originalestimate")
def rField = getFieldById("timetracking_remainingestimate")
def SPField = getFieldByName("Story Points")
def crField = getFieldById(getFieldChanged())
def selectedOption = crField.getValue() as String
if ((selectedOption.contains("GIT Repositories")))
{
    urlField.setHidden(false)
    urlField.setRequired(true)
    aurlField.setHidden(false)
    aurlField.setRequired(true)
    jpField.setHidden(false)
    jpField.setRequired(true)
    NPField.setHidden(true)
    NPField.setRequired(false)
    PKField.setHidden(true)
    PKField.setRequired(false)
    fvField.setHidden(true)
    sField.setHidden(true)
    tField.setHidden(true)
    rField.setHidden(true)
    SPField.setHidden(true)
  
}
else
{
    urlField.setHidden(true)
    urlField.setRequired(false)
    aurlField.setHidden(true)
    aurlField.setRequired(false)
    jpField.setHidden(false)
    jpField.setRequired(true)
    NPField.setHidden(true)
    NPField.setRequired(false)
    PKField.setHidden(true)
    PKField.setRequired(false)
    fvField.setHidden(true)
    sField.setHidden(true)
    tField.setHidden(true)
    rField.setHidden(true)
    SPField.setHidden(true)

Make sure to create a mapping between project and behaviour ID first and then add a field A base don which field B has to be changed or shown

In the above example, based on 

def selectedOption = crField.getValue() as String , I am making other fields visible or hidden or required or not required.

Hope this helps

 

Regards

Chander

Thanks Chander.  Can you help me with the getFieldById() function and the getFieldChanged() function? 

 

In my example I have two fields.  One is called CM_Environment and CM Pre-Production Environments Impacted.  My use case is upon creation of a new issue, if the user selects the value "Pre-Production" in the CM_Environment field, I would like to have the CM Pre-Production Environments Impacted field required.

so I'm thinking I need to write:

def ppeField = getFieldbyName("CM Pre-Production Environment Impacted)

def cmeField = getFieldById(getFieldChanged())

def selected Option = cmeField.getValue() as String

if ((selected Option = "Pre-Production"))

{ppeField.setRequired(true) }

 

does that look right?  I'm not sure how it nows that cmeField is the field "CM_Environment"

 

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
Bridget Sauer
Published yesterday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

65 views 0 4
Read article

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