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 (issue.components.name.contains("Laptop Purchase")) { Laptoppurchase.setRequired(true)} else { Laptoppurchase.setRequired(false) } Please help me here.

2 answers

This widget could not be displayed.

Hello,

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 :(

This widget could not be displayed.

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 ->
component.name == "Laptop Purchase"
}

def laptopPurchase = getFieldByName("Laptop Purchase")

if (isLaptopPurchase) {
laptopPurchase.setRequired(true)
} else {
laptopPurchase.setRequired(false)
}

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 Sign up to answer
Community showcase
Published Apr 22, 2018 in Jira Software

How-to setup a secured Jira Software 7.9.0 on Ubuntu 16.04.4 in less than 30 minutes

...PermissionsStartOnly=true User=www-data Group=www-data ExecStart=/opt/jira/bin/startup.sh ExecStop=/opt/jira/bin/shutdown.sh TimeoutStartSec=120 TimeoutStopSec=600 PrivateTmp=true [Install] WantedBy...

1,490 views 10 12
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