Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scriptrunner Behaviours Require Field On Two Conditions

Justin
Contributor
December 6, 2018

Hello would like to enable behaviors for a bug Issue type that meets the following two conditions on the Resolve screen before the user can transition to Done.

Environment (custom field) = Production
Story Points (system field) = Is not null (i.e greater than zero)

If the environment is not production, don't require story points and allow to transition.

If the environment is production, require story points to be greater than 0 to transition.

 

Here is the script I've been trying to create based on research, but the setRequired portion is not working. Any suggestions would be greatly appreciated.

import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.scriptrunner.runner.util.UserMessageUtil
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def fieldEnv = customFieldManager.getCustomFieldObjectByName ("Environment")
def fieldEnvValue = getCustomFieldValue(fieldEnv)
def fieldSp = customFieldManager.getCustomFieldObjectByName ("Story Points")
def fieldSpValue = getCustomFieldValue (fieldSp)

if (fieldEnvValue == "Production" && fieldSpValue != null) {
        fieldSp.setRequired(true)
        UserMessageUtil.success('Add some points! ')

}else{
        fieldSp.setRequired(false)
}

1 answer

1 accepted

1 vote
Answer accepted
Justin
Contributor
December 6, 2018

Ahh, found a similar question and believe I have this solved. Using the following:

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

def envField = getFieldById(getFieldChanged())
def fieldEnv = getFieldByName ("Environment")
def spField = getFieldByName("Story Points")

String fieldEnvValue = fieldEnv.getValue()
String spIssueValue = spField.getValue()

if (fieldEnvValue == "Production" && spIssueValue != null) {
spField.setRequired(true)
} else {
spField.setRequired(false)
}

Justin
Contributor
December 6, 2018

So this works for having a null value in the Story Points field but not if the value is 0.

I would like it have a number greater than 0. I tried to set my spFieldValue to integer but it's throwing errors. Any thoughts out there?

Tansu Akdeniz
Community Champion
December 10, 2018

Hi @Justin,

Casting String to Int should work. The following code may guide you.

int spIssueInt = Integer.parseInt(spIssueValue);
        if(spIssueInt == 0){
            spField.setError("SP must be greater than 0")
        } else{
            spField.clearError()
        }
Justin
Contributor
December 18, 2018

Ahh, thank you so much! @Tansu Akdeniz

Suggest an answer

Log in or Sign up to answer