Hello, everyone.
I have some Epic and Cascading select field named "Client\Contract" in it with value.
When I create issue and choose Epic Link I need this value been copied to creating form from Epic.
I tried many variants but no result. Maximum I got ID's of needed options and println them to description - it works, but I can't put them on my Cascading select.
Even hardcoded values didn't work. Logs are empty.
Here my server-side script for Epic Link field. Initializer is empty
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.option.Option
try {
def epic = getFieldByName("Epic Link").getFormValue().getAt("key").toString()
MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject(epic)
def CC = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(12902L)
if (issue != null) {
def epicCC = issue.getCustomFieldValue(CC)
def parentValue = epicCC.getAt(null).toString()
def childValue = epicCC.getAt("1").toString()
def CCC = getFieldById("customfield_12902")
def fieldConfig = CC.getRelevantConfig(issue)
def options = ComponentAccessor.getOptionsManager().getOptions(fieldConfig)
for (Option env in options.getRootOptions()) {
if (env.toString() == parentValue) {
def childOptions = env.getChildOptions()
long parenv = env.optionId
long chenv
for (childOption in childOptions){
if (childOption.findAll {it.toString() == childValue}) {
println childOption.optionId
chenv = childOption.optionId
}
}
def desc = getFieldById("description")
desc.setFormValue([parenv, chenv])
CCC.setFormValue([parenv, chenv])
}
}
}
} catch (Exception ex) {
println ex.getStackTrace()
}
What am I doing wrong?
The following works for me ... sets the cascading select field to the epic values when I change the epic link.
import com.atlassian.jira.component.ComponentAccessor
def cfManager = ComponentAccessor.customFieldManager
def issueManager = ComponentAccessor.issueManager
def epicLink = getFieldById(getFieldChanged())
def epicKey = epicLink.value.replaceAll("key:", "")
def epic = issueManager.getIssueObject("$epicKey")
def cf = cfManager.getCustomFieldObjectByName('CascadingSelect')
def ff = getFieldByName('CascadingSelect')
def epicVal = epic.getCustomFieldValue(cf)
ff.setFormValue([epicVal.getAt(null).optionId, epicVal.getAt("1").optionId])
If your issue and epic are in different context and have different option id, then yeah, you might need to use the optionsmanager to get the corresponding id.
Something like this works for me:
import com.atlassian.jira.component.ComponentAccessor
def cfManager = ComponentAccessor.customFieldManager
def issueManager = ComponentAccessor.issueManager
def optionsManager = ComponentAccessor.optionsManager
def epicLink = getFieldById(getFieldChanged())
def epicKey = epicLink.value.replaceAll("key:", "")
def epic = issueManager.getIssueObject("$epicKey")
def cf = cfManager.getCustomFieldObjectByName('CascadingSelect')
def epicVal = epic.getCustomFieldValue(cf)
def fieldConfig = cf.getRelevantConfig(issueContext)
def options = optionsManager.getOptions(fieldConfig)
def parentOption = options.find{it.value == epicVal.getAt(null).value}
def childOption = parentOption.childOptions.find{it.value == epicVal.getAt("1").value}
def ff = getFieldByName('CascadingSelect')
ff.setFormValue([parentOption.optionId, childOption.optionId])
Hello Peter-Dave, thnx for your reply!
I tried your code, with some fixes, but still no values displayed on my cascade select field...
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.Option
def optionsManager = ComponentAccessor.getOptionsManager()
def cfManager = ComponentAccessor.customFieldManager
def issueManager = ComponentAccessor.issueManager
def epicLink = getFieldById(getFieldChanged())
def epicKey = epicLink.value.toString().replaceAll("key:", "")
def epic = issueManager.getIssueObject("$epicKey")
def fieldName = "customfield_12902"
def cf = cfManager.getCustomFieldObject(fieldName)
if (epic != null) {
def epicVal = epic.getCustomFieldValue(cf)
def fieldConfig = cf.getRelevantConfig(issueContext)
def options = optionsManager.getOptions(fieldConfig)
def parentOption = options.find{it.value == epicVal.getAt(null).toString()}
def childOption = parentOption.childOptions.find{it.value == epicVal.getAt("1").toString()}
def ff = getFieldById(fieldName)
ff.setFormValue([parentOption.optionId, childOption.optionId])
println ff.getFormValue()
}
ff.getFormValue at last row displays correct ID's of options, but no values on Create form. I'm confused.
Any suggestions?
@Nic Brough -Adaptavist- , @JamieA , any ideas?) Even if I set real option ID's manually (like it described here https://scriptrunner.adaptavist.com/5.5.7/jira/recipes/behaviours/setting-default-fields.html) it doesn't work
ff.setFormValue([20903, 22829])
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
UPDATE: if set
ff.setFormValue(["20903", "22829"])
instead of
ff.setFormValue([20903, 22829])
then parent option displays on form. Child options refreshed, but None selected.
I think something changed in Jira back logic.
Jira 8.0.2
ScriptRunner 5.5.0.3-jira8
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Ah perhaps... I don't have access to a Jira 8.x environment yet.
If this works:
ff.setFormValue(["20903", "22829"])
Then you can try this:
ff.setFormValue(["$parentOption.optionId", "$childOption.optionId"])
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You misunderstood me.
I mean that
ff.setFormValue(["20903", "22829"])
//OR
ff.setFormValue([parentOption.optionId.toString(), childOption.optionId.toStrin()])
works ONLY for PARENT option. Child option didn't set if exist, it is None all the time.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Are you sure that your two ids are associated correctly to each other?
I just installed a local version of Jira 8.2.2 and latest Scriptrunner.
And I am able to set a cascading select using a very simple behavior.
Here is my test.
Cascade Field Options
Behavior on summary field:
def ff = getFieldByName('Cascade')
def sum = getFieldById('summary').value
def a = sum.split("-")
ff.setFormValue(a)
When I type 100004-10007 in summary, the Cascade field is set correctly.
But if I type 10004-10006, then the behavior your described (parent only populated) is what happens.
So I think the setFormValue is still working in Jira 8.
I think you should run some test and output the option name from your epic and the associated optionid into the log or some other visible text representation then try with those.
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.