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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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."
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.