Conditionally set field value based on other field value on Create transition

Hi,

I need some help with setting a field value conditionally on another field value.

Here's the scenario:

If value in Field A begins with an underscore set Field B to Yes else set Field B to No on the Create transition.

Is that possible? We have Scriptrunner but I'm not a coder so am in the dark.

Thanks, Michael J

1 answer

Hello Michael.

Are these fields custom fields or not? It would change a bit the implementation of the code. If you give me a specific example of what you want I try my best to tell you how to do this.

 

Cheers

Hi Daniel,

Thanks for getting back to me. Field A is Components so not custom, Field B is a radio button custom field with values of Yes or No.

Best,

Michael J

Hello Michael

This is the solution to what you are looking for:

import com.atlassian.jira.component.ComponentAccessor

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

import groovy.transform.BaseScript@BaseScript FieldBehaviours behaviours

import static com.atlassian.jira.issue.IssueFieldConstants.*def varCheck = getFieldById(COMPONENTS)def customFieldvalue = getFieldByName("custom_field")

def optionsManager = ComponentAccessor.getOptionsManager()

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def customField = customFieldManager.getCustomFieldObject(customFieldvalue.getFieldId())

def config = customField.getRelevantConfig(getIssueContext())

def options = optionsManager.getOptions(config)if(varCheck.value.toString().startsWith("_")){

    def optionToSelect = options.find { it.value == "yes" }

    customFieldvalue.setFormValue(optionToSelect.optionId)}

else{

    def optionToSelect = options.find { it.value == "no" }

    customFieldvalue.setFormValue(optionToSelect.optionId)}

Mind you, you will need to replace "custom_field" with the name of the field that contains your custom field checkbox

Also I would recomend you check out the documentation about behaviours that you can find in the following link:

https://scriptrunner.adaptavist.com/latest/jira/recipes/behaviours/setting-default-fields.html

Hope this helped, if we can do anything else for you let us know.

Cheers

Wow, thank you so much! This is my first behavior so please be patient. I followed the instructions here to set it up, https://scriptrunner.adaptavist.com/latest/jira/behaviours-overview.html. But I'm getting an error on this line:
{code}
import groovy.transform.BaseScript@BaseScript FieldBehaviours behaviours
{code}
The error is "Compilation failure: startup failed: Script1.groovy: 7: unexpected token: @ @ line 7, column 35. import groovy.transform.BaseScript@BaseScript FieldBehaviours behaviours ^ 1 error."

Hello Michael.

That is happening because the @ needs to be in a new line.

Your code should look like this:

import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours

Hope this helped out

Hi Daniel,

I appreciate your help but when I pull the @ out to its own line, I get a new error. I've pasted the code I'm using below.

import com.atlassian.jira.component.ComponentAccessor

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

import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours

import static com.atlassian.jira.issue.IssueFieldConstants.*def varCheck = getFieldById(COMPONENTS)def customFieldvalue = getFieldByName("For Exhibition or Publication")

def optionsManager = ComponentAccessor.getOptionsManager()

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def customField = customFieldManager.getCustomFieldObject(customFieldvalue.getFieldId())

def config = customField.getRelevantConfig(getIssueContext())

def options = optionsManager.getOptions(config)if(varCheck.value.toString().startsWith("_")){

    def optionToSelect = options.find { it.value == "yes" }

    customFieldvalue.setFormValue(optionToSelect.optionId)}

else{

    def optionToSelect = options.find { it.value == "no" }

    customFieldvalue.setFormValue(optionToSelect.optionId)}


And this is the error.

Compilation failure: startup failed: Script1.groovy: 11: expecting EOF, found 'def' @ line 11, column 61. ra.issue.IssueFieldConstants.*def varChe ^ 1 error


Thank you,

Michael J

Hi Daniel,

I'm still having trouble getting this to work. Here is my code and I have it mapped to one issue type in a project. I don't have any Fields defined for the Behaviour so maybe that's the problem. Not sure.

import com.atlassian.jira.component.ComponentAccessor

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

import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours

import static com.atlassian.jira.issue.IssueFieldConstants.*

def varCheck = getFieldById(COMPONENTS)
    
def customFieldvalue = getFieldByName("For Exhibition or Publication")

def optionsManager = ComponentAccessor.getOptionsManager()

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def customField = customFieldManager.getCustomFieldObject(customFieldvalue.getFieldId())

def config = customField.getRelevantConfig(getIssueContext())

def options = optionsManager.getOptions(config)

if(varCheck.value.toString().startsWith("_")){

def optionToSelect = options.find { it.value == "Yes" }

    customFieldvalue.setFormValue(optionToSelect.optionId)}

else{

def optionToSelect = options.find { it.value == "No" }

    customFieldvalue.setFormValue(optionToSelect.optionId)}

Thanks for any help!

Best, Michael J

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published yesterday in Jira Service Desk

Wy are we still using email for Service Desk workflows?

...attest to the experience of an urgent approval that gets lost in the boss’s inbox and requires that special “Please Approve” email or text message. In an age where we have distributed teams...

104 views 0 2
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