Reputation: 85
I'm trying to create a behavior in Jira that checks whether a field ("Call #1") is not empty and if so it makes two other fields required. it was written in ScriptRunner (Groovy)
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()
// Get the Call #1 Field
FormField call1 = getFieldByName('Call #1')
// Get the Role #1 Field
FormField role1 = getFieldByName('Role #1')
// Get the Call result #1 Field
FormField callResult1 = getFieldByName('Call result #1')
if (call1.getValue() != null)
{
role1.setRequired(true)
callResult1.setRequired(true)
}
I'm not sure why but it's making both "Role #1" and "result #1" fields mandatory from the beginning even though the "Call #1" is empty. Anyone knows why?
Upvotes: 0
Views: 1718
Reputation: 1560
You are so close.
If you write behaviour, you should consider two things.
One of them is the Initializer script, which runs when the screen first loaded.
The other one is script for the field. (In your case, the Call #1
field); which runs when the field's value changed.
Also, you should remove the required
field at start of your script.
So the following script should be placed both at Initializer script and the field's script:
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()
// Get the Call #1 Field
def call1 = getFieldByName('Call #1')
// Get the Role #1 Field
def role1 = getFieldByName('Role #1')
// Get the Call result #1 Field
def callResult1 = getFieldByName('Call result #1')
role1.required = false
callResult1.required = false
// Also if you log the call1's value, you can check the actual value from the Jira's log
def call1Value = call1.getValue()
log.warn(call1Value)
if (call1Value != null || !call1Value.toString().trim().isEmpty()) // and if it is a String field, check the emptiness
{
role1.required = true
callResult1.required = true
}
Upvotes: 1