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.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.