jira scriptrunner set form value for select( single choice)

I created 3 custom field(A,B,C) under issue. They are all single choice. 

Options:

A: 1, 2, 3

B: a, b, c

C: i, ii, iii

Goal, Once custom field A value set to 1, custom B,C will auto filled to a and i.

I did try follow online doc "Setting Defaults for Selects etc", got error below

error:

Can not find matching method for setFormValue 

Below is my code

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Options

def A = getCustomFieldValue(12506)
def B = getCustomFieldValue(12501)
def C = getCustomFieldValue(12507)


def customFieldManager = ComponentAccessor.getComponent(CustomFieldManager);
def optionsManager1 = ComponentAccessor.getComponent(OptionsManager);
//def customField1 = customFieldManager.getCustomFieldObjectByName("A")
Options options1 = optionsManager1.getOptions(customField.getConfigurationSchemes().first().getOneAndOnlyConfig());

def optionsManager = ComponentAccessor.getOptionsManager()
def customField = customFieldManager.getCustomFieldObject("12501")
def config = customField.getRelevantConfig(issue)
def options = optionsManager.getOptions(config)
def optionToSelectBi = options.find { it.value == "1" }
def optionToSelectBii = options.find { it.value == "2" }
def optionToSelectBiii = options.find { it.value == "3" }

def optionsManagertestv = ComponentAccessor.getOptionsManager()
def customFieldtestv = customFieldManager.getCustomFieldObject("12507")
def configtestv = customFieldtestv.getRelevantConfig(issue)
def optionstestv = optionsManagertestv .getOptions(configtestv)
def optionToSelectCi = optionstestv .find { it.value == "a" }
def optionToSelectCii = optionstestv .find { it.value == "b" }
def optionToSelectCviii = optionstestv .find { it.value == "c" }

if(A == "i"){
customField.setFormValue(optionToSelectBi.optionId)
customField.setFormValue(optionToSelectCi.optionId)
} else if(A == "ii"){
customField.setFormValue(optionToSelectBii.optionId)
customField.setFormValue(optionToSelectCii.optionId)
} else if(A == "iii"){
customField.setFormValue(optionToSelectBiii.optionId)
customField.setFormValue(optionToSelectCiii.optionId)
}else{

}

2 answers

1 accepted

0 votes
Accepted answer

Hi Qi Jiang,

The general format for setting a select list in a behaviour is as follows:

import com.atlassian.jira.component.ComponentAccessor

def fieldName = "SelectListA"
def field = getFieldByName(fieldName)
def optionsManager = ComponentAccessor.getOptionsManager()

def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName(fieldName)
def fieldConfig = customField.getRelevantConfig(getIssueContext())

def options = optionsManager.getOptions(fieldConfig)
def option = options.find {it.value == "AAA"}

field.setFormValue(option.optionId)

 I have attempted to fix your script, but I have not tested it.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager

def customFieldManager = ComponentAccessor.getComponent(CustomFieldManager)
def optionsManager = ComponentAccessor.getOptionsManager()

def A = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("12506"))
def B = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("12501"))
def C = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("12507"))

def customField = customFieldManager.getCustomFieldObject("12501")
def config = customField.getRelevantConfig(issue)
def options = optionsManager.getOptions(config)
def optionToSelectBi = options.find { it.value == "1" }
def optionToSelectBii = options.find { it.value == "2" }
def optionToSelectBiii = options.find { it.value == "3" }

def customFieldtestv = customFieldManager.getCustomFieldObject("12507")
def configtestv = customFieldtestv.getRelevantConfig(issue)
def optionstestv = optionsManager.getOptions(configtestv)
def optionToSelectCi = optionstestv.find { it.value == "a" }
def optionToSelectCii = optionstestv.find { it.value == "b" }
def optionToSelectCviii = optionstestv.find { it.value == "c" }

if(A == "i"){
customField.setFormValue(optionToSelectBi.optionId)
customField.setFormValue(optionToSelectCi.optionId)
} else if(A == "ii"){
customField.setFormValue(optionToSelectBii.optionId)
customField.setFormValue(optionToSelectCii.optionId)
} else if(A == "iii"){
customField.setFormValue(optionToSelectBiii.optionId)
customField.setFormValue(optionToSelectCviii.optionId)
}else {
return
}

Thanks for reply, but looks like sample code you gave me is just set default value for on select list. What I need is:

For select list A(a,b,c),B(I,ii,iii),C(1,2,3)

Once A selected a, B and C will auto fill with ii and 2. Could you provide with me more advise on this please.

Thanks Qi Jiang

I wrote a script that seems to accomplish what you need. First, I get the select list options for Select List B. Then, I get the options for Select List C.

I then check the value of Select List A.

  • If the value of Select List A is 'a', then set value in Select List B and Select List C.
  • If the value of Select List A is 'b', then set values in Select List B and Select List C.
  • If the value of Select List A is 'c', then set values in Select List B and Select List C.
import com.atlassian.jira.component.ComponentAccessor

def customFieldA = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName(fieldName)
def fieldB = getFieldById(12501)
def fieldC = getFieldById(12507)

def optionsManager = ComponentAccessor.getOptionsManager()

def customFieldB = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName(fieldName)
def fieldConfigB = customFieldB.getRelevantConfig(getIssueContext())
def optionsB = optionsManager.getOptions(fieldConfigB)
def optionBI = optionsB.find {it.value == "I"}
def optionBII = optionsB.find {it.value == "II"}
def optionBIII = optionsB.find {it.value == "III"}

def customFieldC = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName(fieldName)
def fieldConfigC = customFieldC.getRelevantConfig(getIssueContext())
def optionsC = optionsManager.getOptions(fieldConfigC)
def optionC1 = optionsC.find {it.value == "1"}
def optionC2 = optionsC.find {it.value == "2"}
def optionC3 = optionsC.find {it.value == "3"}

if (issue.getCustomFieldValue(customFieldA) == "a") {
fieldB.setFormValue(optionBI.optionId)
fieldC.setFormValue(optionC1.optionId)
}
else if (issue.getCustomFieldValue(customFieldA) == "b") {
fieldB.setFormValue(optionBII.optionId)
fieldC.setFormValue(optionC2.optionId)
}
else if (issue.getCustomFieldValue(customFieldA) == "c") {
fieldB.setFormValue(optionBIII.optionId)
fieldC.setFormValue(optionC3.optionId)
}

Josh,

I got error on line 4, 5,23,24,25,27,28,29,31,32,33

Can not find matching method on line 4,5,24 25,28,29, 32 33Capture.PNGPlease advise.

Thanks

var is undeclared on 23 27 31

Hi Qi Jiang,

Sorry for that. I made some improvements to the script. All you need to do is change the script and put in the correct name of your Select List custom fields.

import com.atlassian.jira.component.ComponentAccessor

def fieldA = getFieldByName("Name of A here")
def fieldB = getFieldByName("Name of B here")
def fieldC = getFieldByName("Name of C here")

def optionsManager = ComponentAccessor.getOptionsManager()

def customFieldB = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Name of B here")
def fieldConfigB = customFieldB.getRelevantConfig(getIssueContext())
def optionsB = optionsManager.getOptions(fieldConfigB)
def optionBI = optionsB.find {it.value == "I"}
def optionBII = optionsB.find {it.value == "II"}
def optionBIII = optionsB.find {it.value == "III"}

def customFieldC = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Name of C here")
def fieldConfigC = customFieldC.getRelevantConfig(getIssueContext())
def optionsC = optionsManager.getOptions(fieldConfigC)
def optionC1 = optionsC.find {it.value == "1"}
def optionC2 = optionsC.find {it.value == "2"}
def optionC3 = optionsC.find {it.value == "3"}

if (fieldA.getValue() == "a") {
fieldB.setFormValue(optionBI.optionId)
fieldC.setFormValue(optionC1.optionId)
}
else if (fieldA.getValue() == "b") {
fieldB.setFormValue(optionBII.optionId)
fieldC.setFormValue(optionC2.optionId)
}
else if (fieldA.getValue() == "c") {
fieldB.setFormValue(optionBIII.optionId)
fieldC.setFormValue(optionC3.optionId)
}

I tested this script with my Select List names and it appears to work:

Screen Shot 2017-08-08 at 12.28.57 PM.png

As @Jenna Davis said, this should go in behaviour attached to the Select List A field.

@Joshua Yamdogo [Adaptavist]

Hi Joshua

I am trying to achieve something similar. I have 3 risk field , Risk Consequence, Risk Likelihood, Risk Rating.

I am trying to automate the Rating from the Consequence * Likelihood. 

Is this possible to write a server side behaviour script to do this and if so should the script be placed on all 3 fields? 

Hi @Richard Duffy,

If you want to accomplish this with a behaviour, you'd most likely want to have a behaviour on the Consequence and Likelihood fields. That would ensure that changes to either field would get picked up and the value would be calculated correctly.

Regards,

Josh

Like 1 person likes this

Hi Joshua

Thanks my script is working! I did have one issue, I could not get it working like this 

if (cfRiskCon.getValue() == "Minimal" && cfRiskLike.getValue() == "Rare" || "Unlikely" || "Possible" || "Likely" || "Almost Certain" ) {
cfRiskRat.setFormValue(optionC1.optionId)

so i had to do this 

if (cfRiskCon.getValue() == "Minimal" && cfRiskLike.getValue() == "Rare") {
cfRiskRat.setFormValue(optionC1.optionId)
}
else if (cfRiskCon.getValue() == "Minimal" && cfRiskLike.getValue() == "Unlikely") {
cfRiskRat.setFormValue(optionC1.optionId)
}
else if (cfRiskCon.getValue() == "Minimal" && cfRiskLike.getValue() == "Possible") {
cfRiskRat.setFormValue(optionC1.optionId)
}
else if (cfRiskCon.getValue() == "Minimal" && cfRiskLike.getValue() == "Likely") {
cfRiskRat.setFormValue(optionC1.optionId)
}
else if (cfRiskCon.getValue() == "Minimal" && cfRiskLike.getValue() == "Almost Certain") {
cfRiskRat.setFormValue(optionC1.optionId)
}

 

Its not a big issue but just not very tidy the second way, Can you identify why the top way fails? I feel like its something really simple like  a missing bracket 

 

Thanks again

Richard  

Hi Richard, 

Yes, small error with chaining the "OR" operators together. You would need to write the first piece of code like this instead:

if (cfRiskCon.getValue() == "Minimal" && cfRiskLike.getValue() == "Rare" || cfRiskLike.getValue() == "Unlikely" || cfRiskLike.getValue() == "Possible" || cfRiskLike.getValue() == "Likely" || cfRiskLike.getValue() == "Almost Certain") {
cfRiskRat.setFormValue(optionC1.optionId)
}

Regards,

Josh

Like 1 person likes this
1 vote
Jenna Davis Community Champion Aug 08, 2017

Hello, 

Are you setting this up as a behaviour? If so, please send me a screenshot of your behaviour configurations page. If not, how are you implementing your code?

If this is indeed a behaviour and you are using this code as a file, not directly in the script console, you need to include this at the top of the code file:

import com.onresolve.jira.groovy.user.FieldBehaviours 
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

This will also give you code completion for behaviours if you have a development environment set up. You can read more here

Please send me a screenshot and let me know how you are using implementing the code. If you are using a behaviour linked-file, please try that code snippet. If you're still having trouble I will help you get it working from there. :)

Jenna

I created a new field of type scripted field, and added code in the script console.

Jenna Davis Community Champion Aug 08, 2017

You should probably be using a behaviour to accomplish this. A scripted field will not let you use the 'setFormValue' method which explains why you received the error. Scripted Fields are more for running calculations or pulling information from an outside source, whereas you can simply use a behaviour to set the select the options you want during issue creation. For your particular use case I believe a behaviour is more appropriate overall. 

The example Josh provided above should work for what you're wanting to accomplish. The script will make the other fields auto-fill when an option in field A is selected. 

If you need more assistance, please let us know. :)

Jenna

Cool I will work with Josh =)

@Joshua Yamdogo [Adaptavist]

Hi Joshua,

If I want to apply this code for multi-select field, what are the changes I need to make?

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 29, 2018 in Marketplace Apps

How to set up an incident workflow from the VP of Engineering at Sentry

Hey Atlassian community, I help lead engineering at Sentry, an open-source error-tracking and monitoring tool that integrates with Jira. We started using Jira Software Cloud internally last year, a...

1,405 views 0 8
Read article

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