making field mandatory when one specific component is selected

arathi.gopinath June 5, 2017

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

1 vote
Thomas Venekamp June 6, 2017

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!")
 }
}
adammarkham
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.
June 6, 2017

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

l June 7, 2017

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

0 votes
adammarkham
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.
June 6, 2017

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.

 

l June 7, 2017

The script provided is not working :(

adammarkham
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.
June 7, 2017

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.

l June 7, 2017

Hi Adam,

 

Still the same.

adammarkham
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.
June 7, 2017

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?

l June 7, 2017

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}

 

l June 7, 2017

select list was the type and version is 3.1.4

 

adammarkham
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.
June 7, 2017

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