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

This widget could not be displayed.

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
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

216 views 3 0
Join discussion

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