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

Michael Johnson
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 10, 2017

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

0 votes
Daniel Yelamos [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 11, 2017

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

Michael Johnson
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 11, 2017

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

Daniel Yelamos [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 11, 2017

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

Michael Johnson
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 11, 2017

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."

Daniel Yelamos [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 12, 2017

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

Michael Johnson
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 12, 2017

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

Michael Johnson
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 18, 2017

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