Hi folks,
I'm not a developer and would love some input - my head is spinning at this point.
My use case is the following:
If a dropdown custom field has the value "Regression Test", then check the box on the "Add to Regression Set" checkbox.
I put a post function on the create step as the last step. I had the following code strung together from multiple examples and even though it wasn't failing, it wasn't checking the box.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
import com.atlassian.jira.issue.customfields.manager.OptionsManager
log.setLevel(org.apache.log4j.Level.DEBUG)
final customFieldName = "Test Type"
//Get custom field issue with this name
def customFieldManager = ComponentAccessor.customFieldManager
def customField = customFieldManager.getCustomFieldObjects(issue).findByName(customFieldName)
assert customField: "Could not find custom field with name $customFieldName"
def customFieldVal = issue.getCustomFieldValue(customField) as LazyLoadedOption
log.debug "Test Type is ${customFieldVal}"
//set "Add to Regression Set' as field to be updated- all from my data conversion issue type when updating checkbox fields
def optionsManager = ComponentAccessor.getComponent(OptionsManager)
def customFieldManager2 = ComponentAccessor.getCustomFieldManager()
//Update "Add to Regression Set" depending on the custom field value
switch (customFieldVal?.value) {
case "Regression Test":
log.debug "Add to Regression Set checkbox should be checked"
def cf = customFieldManager2.getCustomFieldObjectByName("Add to Regression Set") //[0]
def fieldConfig = cf.getRelevantConfig(issue)
def options = optionsManager.getOptions(fieldConfig).getOptionForValue("Yes", null)
//def optionsToSet = options.findAll {it.value in ["Yes",null]}
issue.setCustomFieldValue(cf, options)
log.debug "Add to Regression Set value is ${options}"
log.debug "Add to Regression Set checkbox was checked"
break
default:
log.debug "No need to update the Add to Regression Set checkbox"
break
}
I then found this post: ScriptRunner Modifying Checkbox values on transition post-function
and thought maybe my code could be a whole lot more simple, but when I use the following,
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.config.FieldConfig
def customFieldManager = ComponentManager.getInstance().getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjectByName("Add to Regression Set")
def fieldConfig = cf.getRelevantConfig(issue)
value = ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find { it.toString() == 'Yes' }
if (cfValues['Test Type']?.value == 'Regression Test') {
issue.setCustomFieldValue(cf, value)
}
I get an error before I can save on import com.atlassian.jira.ComponentManager saying to resolve the issue class.
I'd love some help. I thought this would be easier than it has turned out to be. :) (That's always the case...)
Thanks!!
Laurie
Hi @LaurieC Instead of Post function, you can use behaviour on Select list field and if you don't allow users to edit checkbox field then make checkbox field read only as well using Behaviour. Not sure why post function is not updating.
Apply below script in Behaviour on Select List field >> Server Side script. It should work.
import com.atlassian.jira.component.ComponentAccessor
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def accuracy = getFieldById(getFieldChanged())
def selectedOption = accuracy.getValue() as String
def checkbox = getFieldByName("ABIERTO")
def customField = customFieldManager.getCustomFieldObject(checkbox.getFieldId())
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)
def optionsToSelect = options.findAll { it.value in ["Yes"] } //checkbox field values
def optionsToSelect1 = options.findAll { it.value in [null] }
//def optionsToSelect = options.findAll { it.value in ["No"] } //some other value
if (selectedOption == "Satisfied") {
checkbox.setFormValue(optionsToSelect*.optionId)
}
else {
checkbox.setFormValue(optionsToSelect1*.optionId)
}
//Replace ABIERTO with your checkbox field name and Values as well
Thanks
You are getting error because CompoentManager has been moved.
import com.atlassian.jira.ComponentManager
replace with
import com.atlassian.jira.component.pico.ComponentManager
https://scriptrunner.adaptavist.com/latest/jira/releases/UpgradingToJira8.html
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks @Vikrant Yadav for your help on this!
I'm reading up on behaviours and think this isn't a good place for our organization to use them since our teams often bulk load issues of this type and it seems that is one limitation of behaviours.
I'm hoping still to be able to do this via a post function.
Thanks for the link to the update information for Jira 8. Interestingly enough, when I try to place the code with
import com.atlassian.jira.component.pico.ComponentManager
it throws an error (unable to resolve class) and I'm not getting that issue anywhere else that I haven't updated that line and those post functions are still working. I definitely need to dig into all of my post functions to make sure they are current.
I'm wondering if there is something else going on here? Any other thoughts?
Thanks again for your help!!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @LaurieC Try below script in post function.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.Issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getComponent(OptionsManager)
def Accuracy = customFieldManager.getCustomFieldObjectByName("Accuracy")//select list field
def cFieldValue = issue.getCustomFieldValue(Accuracy) as String
def cf = customFieldManager.getCustomFieldObjectByName("ABIERTO") // Checkboxes is the NAME of my custom field
def fieldConfig = cf.getRelevantConfig(issue)
def option = optionsManager.getOptions(fieldConfig)
def optionsToSelect = option.findAll { it.value in ["Yes"] } //checkbox field values
def optionsToSelect1 = option.findAll { it.value in ["No"] }
if (cFieldValue == "Satisfied") {
issue.setCustomFieldValue(cf, [optionsToSelect])
}
else {
issue.setCustomFieldValue(cf, [optionsToSelect1])
}
//Replace ABIERTO with your checkbox field name and Values as well
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks so much @Vikrant Yadav .
I'm getting the same result. It thinks it is executing, but nothing is happening.
Here's the code I used:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.Issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getComponent(OptionsManager)
log.setLevel(org.apache.log4j.Level.DEBUG)
def TestType = customFieldManager.getCustomFieldObjectByName("Test Type")//select list field
def cFieldValue = issue.getCustomFieldValue(TestType) as String
log.debug "Test type is ${cFieldValue}"
def cf = customFieldManager.getCustomFieldObjectByName("RS") // Checkboxes is the NAME of my custom field
def fieldConfig = cf.getRelevantConfig(issue)
def option = optionsManager.getOptions(fieldConfig)
def optionsToSelect = option.findAll { it.value in ["Yes"] } //checkbox field values
def optionsToSelect1 = option.findAll { it.value in [null] }
if (cFieldValue == "Regression Test") {
issue.setCustomFieldValue(cf, [optionsToSelect])
log.debug "Value of Add to Regression Set should be ${optionsToSelect}"
}
else {
issue.setCustomFieldValue(cf, [optionsToSelect1])
log.debug "No need to update the Add to Regression Set checkbox"
}
Thinking of other things that might be impacting it:
Location in the post functions? I've tried this as the 1st step in the post functions and it fails (I'm guessing because the issue hasn't been created.) I've moved this to the 2nd step. I also had it as the last step.
Any other thoughts? Any ideas on how I might further troubleshoot the problem with logs?
Thanks so much for your time!!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hey @Martin Bayer _MoroSystems_ s_r_o__ Need your help mate! No sure why script is not working in post function. Not throwing any error in logs. Kindly review the script and suggest what we are doing wrong. 🤔
Thanks
V Y
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @LaurieC Good News for you. I fixed the issue in the script and working fine now in Post function :)
Give it a try. Let me know the result.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.Issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getComponent(OptionsManager)
def Accuracy = customFieldManager.getCustomFieldObjectByName("Accuracy")//select list field
def cFieldValue = issue.getCustomFieldValue(Accuracy) as String
def cf = customFieldManager.getCustomFieldObjectByName("ABIERTO") // Checkboxes custom field
def fieldConfig = cf.getRelevantConfig(issue)
def value = ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find {
it.toString() == 'Yes'
}
def value1 = ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find {
it.toString() == 'No'
}
if (cFieldValue == "Satisfied") {
issue.setCustomFieldValue(cf, [value])
}
else {
issue.setCustomFieldValue(cf, [value1])
}
//Replace ABIERTO with your checkbox field name and Values as well
Thanks
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks again @Vikrant Yadav
Unfortunately, it still isn't working... :(
I took the whole thing out and replaced it with Adaptivist's code for updating a checkbox - without an if statement - and that isn't working either... what am I missing?
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.manager.OptionsManager
def optionsManager = ComponentAccessor.getComponent(OptionsManager)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjectByName("Add to Regression Set") // Checkboxes is the NAME of my custom field
def fieldConfig = cf.getRelevantConfig(issue)
def option = optionsManager.getOptions(fieldConfig).getOptionForValue("Yes", null)
issue.setCustomFieldValue(cf, [option])
My field is definitely a checkbox:
I've double-checked that there is no space after the "Yes" or in the name of the field
Feels like it is time for a support ticket....
I so appreciate your help and time @Vikrant Yadav !!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @LaurieC It's weird same script working fine on my side. Updating checkbox field based on select list field.
Check script runner version, i am 6.28.
Check wheater there is some condition on create issue transition ?
field is added on the create screen and view screen, right?
Thanks
VY
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You are so helpful @Vikrant Yadav ! Thanks for the video!
I wonder if it is our version. We're on an older version of ScriptRunner (6.12) and older Jira (8.5.9).
And the crazy thing is that I have a post function on another workflow to set the flag on a checkbox and it is working fine. I even copied that code and put it here just to see if I could check the box and it won't - even without the if statement.
I do have the field on the create, view, edit screens. I do have a condition on the create transition (which this is on) that requires that the Test Type field be populated - and that is the field I need for this if statement...
I have a ticket into Adaptavist. I'm not sure what else to do at this point.
Thanks!!
Laurie
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @LaurieC , @Vikrant Yadav , sorry for late response. Unfortunatelly I do not have server/DC environment to test your script but I suggest you to put the postfunction as the last and then add one more postfunction Store issue:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hey @Martin Bayer _MoroSystems_ s_r_o__ thanks a lot for your response. @LaurieC Kindly follow the solution suggested by Martin. It definitely works for you 😊.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Vikrant Yadav and @Martin Bayer _MoroSystems_ s_r_o__ ,
Thanks so much for your help!
I tried this - put the script step as the last step and added a store step. It won't let me change the order for the store step so it is 2nd.
Still no go. I tried putting my script step before the store step and still no go.
Adaptavist has been helping me look at it too. So far, no luck. strangely, the logging in the script seems to imply it is working
021-06-28 18:41:09,655 DEBUG [runner.ScriptBindingsManager]: Test type is Regression Test 2021-06-28 18:41:09,655 DEBUG [runner.ScriptBindingsManager]: IF entered 2021-06-28 18:41:09,655 DEBUG [runner.ScriptBindingsManager]: Checkbox field value before: null 2021-06-28 18:41:09,655 DEBUG [runner.ScriptBindingsManager]: Value of Add to Regression Set should be Yes 2021-06-28 18:41:09,655 DEBUG [runner.ScriptBindingsManager]: Checkbox field value after: [Yes]
But when I run a script in the console to see the value of the field, it comes back null.
I'm going to move my configuration to production shortly and put it on a demo project. I'm hoping it works there, although that makes me a bit uncomfortable about what is going on with my DEV environment...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @LaurieC didit workon PROD?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello, anybody was able to make this work? My scenario requires multiple checkbox options to be selected on the form. Please help.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Boriz Agustin What is your scenario ? Are you trying to apply a Behaviour ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello @Vikrant Yadav - Yes I am. Currently, my script is set to only show selected options based on issue but I would also need to have some of the options be selected by default(based on issue type) and possibly have them greyed out.
My question is, how do we set more than one value on a checkbox?
if (underlyingIssue?.issueType.name == "Proposal") {
formField.setFieldOptions(Proposal).setHidden(false)
formField.setFieldOptions(Proposal).setRequired(true)
formField.setFormValue("RFP Compliance Matrix") << I need to add one more option here
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Boriz Agustin Thanks for sharing the script, you can try below script for updating Checkbox field options based on Issue Type :-
import com.atlassian.jira.component.ComponentAccessor
def issuetype = getIssueContext().getIssueType().name
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def test_checkbox = getFieldByName("Test Checkbox")
def customField = customFieldManager.getCustomFieldObject(test_checkbox.getFieldId())
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)
def optionsToSelect = options.findAll { it.value in ["US","IN","NZ"] }
def optionsToSelect1 = options.findAll { it.value in ["UK","UAE","SA"] }
if (issuetype.contains("Bug")){
test_checkbox.setFormValue(optionsToSelect*.optionId)
test_checkbox.setReadOnly(true) //make field read only so that user can't select other options
} else if (issuetype.contains("Story")){
test_checkbox.setFormValue(optionsToSelect1*.optionId)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.