making field mandatory when one specific component is selected

While creating one issue in a project I want to make a field mandatory when I select the component as "Laptop Purchase". I have tried the code as below, but didn't work for me. import com.atlassian.jira.component.ComponentAccessordef optionsManager = ComponentAccessor.getOptionsManager()def customFieldManager = ComponentAccessor.getCustomFieldManager()def Laptoppurchase = getFieldById("customfield_29690")if ("Laptop Purchase")) { Laptoppurchase.setRequired(true)} else { Laptoppurchase.setRequired(false) } Please help me here.

2 answers


you can write a validator for the task and use it during the 'Create' transition.

The following code will check, if a component named "Laptop Purchase" is used and will mark the field "customfield_29690" as required.

import com.opensymphony.workflow.InvalidInputException

def currentComponents = issue.getComponents()

for (c in currentComponents) {
 if (c.getName() == "Laptop Purchase") {
  invalidInputException = new InvalidInputException("customfield_29690", "This field is required!")

This will also work if you only want it to check after submitting the form.

I Have added the script in the validator but it didn't work for me :(

You can do this with following behaviours script:

import com.atlassian.jira.bc.project.component.ProjectComponent

def components = getFieldById(getFieldChanged())

def isLaptopPurchase = components.value?.any { ProjectComponent component -> == "Laptop Purchase"

def laptopPurchase = getFieldByName("Laptop Purchase")

if (isLaptopPurchase) {
} else {

You'll need to attach this as a server-side validator script to the "Components" field.

Every time there is a change to the components field this will get triggered and check if the component "Laptop Purchase" has been selected. If so the other field will be marked as required.

Hope this helps.


The script provided is not working :(

Apologies, I've edited the script I provided and simplified it a bit.

It should work for you now. Let us know how you get on with that.

Hi Adam,


Still the same.

Could you please attach a screenshot of your behaviours configuration please.

What is the type of the custom field "Laptop Purchase"?

Also what version of ScriptRunner and JIRA are you using?

Irt  worked after using the validator script as below

import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def Laptoppurchase = customFieldManager.getCustomFieldObjectByName("customfield_29690")
if (issue.components*.name.contains("Laptop Purchase")) {
if (issue.getCustomFieldValue(Laptoppurchase)) {true}
else {false}
else {true}


select list was the type and version is 3.1.4


That's an old version. It's probably a bug which has been fixed in future releases. Glad to hear you were able to find a workaround.

Suggest an answer

Log in or Join to answer
Community showcase
Teodora [Botron]
Published Feb 15, 2018 in Marketplace Apps

Jira Inferno: The Nine Circles of Jira Administration Hell

If you spend enough time as a Jira admin - whether you are managing a single, mid-sized instance, a large enterprise one or juggling multiple instances at once - you will eventually find yourself in ...

1,183 views 6 19
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot