jira scriptrunner set form value for select( single choice)

Qi Jiang August 8, 2017

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{

}

3 answers

1 accepted

2 votes
Answer accepted
Joshua Yamdogo @ 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.
August 8, 2017

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
}
Qi Jiang August 8, 2017

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

Joshua Yamdogo @ 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.
August 8, 2017

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)
}
Qi Jiang August 8, 2017

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

Joshua Yamdogo @ 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.
August 8, 2017

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.

Like Anna Protopapa likes this
Richard Duffy December 11, 2018

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? 

Joshua Yamdogo @ 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.
December 12, 2018

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 Richard Duffy likes this
Richard Duffy December 12, 2018

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  

Joshua Yamdogo @ 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.
December 13, 2018

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 Richard Duffy likes this
RG December 4, 2019

Hi Joshua,

I am writing Post Function script to set value of system field based on other two custom field values. I have 3 fields  America, India and Europe

 

America is Cascaded Single Choice Field

India is Single Choice

Europe is Single Choice

 

Cascaded Field for America are FA1 and FA2

FA1 values : A, B, C
FA2 values: Values for A (A1,A2,A3)
Values for B (B1,B2,B3)
Values for C (C1,C2,C3)

Values of India : X, Y, Z
Values of Europe : AA, BB, CC, DD

I am setting issue security Lock in Workflow as Post Function via scriptrunner (cfValues Script). Below are the conditions:

1. Set Europe value to AA (condition applies) when user select below America Values


America (FA1)  ='A'
America (FA1) = ‘B’
America (FA1) = ‘C’

2. Set Europe value to BB if user select India Values to India value X

India = X

3. Set Europe value to DD if user select India value X and America value to A, B and C

India= X
and
America (FA1)  = A
America (FA1)  = B

America (FA1)  = C

4. Set Europe value to CC if user select India value Y

India = Y

Please help me ASAP, as its urgent and we need to implement this on our PROD.

------------------------------------
Tried with below cfValues script functions:

def list = ['A','B','C']
cfValues['America']?.get(null)?.value in list

cfValues['India']?.value == 'X'

def list = ['A','B']
cfValues['America']?.get(null)?.value in list && cfValues['India']?.value == 'X'

def list = ['Y']
cfValues['India']?.value in list

 

All are working except condition 3. In condition 3rd issue taking 2nd condition value.

1 vote
Vasanta Kumaar October 9, 2020

Hi Joshua Yamdogo @ Adaptavist ,

 

I am trying  to set a Dropdown field with a value Reset Application Account when we update the Label field with the Label Reset_password_AD_RPA.

I tried with below script in behavior but the dropdown field not getting updated.

def firstField = getFieldById("Labels")
def secondField = getFieldById("customfield_17350")

String firstFieldVal = firstField.getValue()

if (firstFieldVal.equals("Reset_password_AD_RPA")) {
secondField.setFormValue(21540)
} else {
secondField.setFormValue(null)
}

Can you help me on this.

 

Thanks,

Vasantakumaar 

1 vote
Jenna Davis
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.
August 8, 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

Qi Jiang August 8, 2017

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

Jenna Davis
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.
August 8, 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

Qi Jiang August 8, 2017

Cool I will work with Josh =)

Qi Jiang May 31, 2018

Joshua Yamdogo @ Adaptavist

Hi Joshua,

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

Zhuxin Wang November 19, 2019

Hi there,

  How did you set multi-select field by using parameter?

  As my test,

    setFormValue([1,2,3]) can work, but if use parameter it will be       setFormValue(['1,2,3']), because it's a string type, so it will include '', it can't work, so how did you set multi-select field?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events