Scriptrunner Behaviour - Set cf label field based on another cf label value

Hunter1428 May 3, 2021

Hi all,

I would like another custom label field to set it's value based on another custom label field.

Here is my current code example:

def thisField = getFieldById(getFieldChanged())

def A= getFieldByName("A")

if (thisField.getValue() in (["123"]))
{
A.setFormValue("568")
}

 This seems to work well for every other field type except custom label field :(

Any ideas?

1 answer

0 votes
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 3, 2021

When in doubt ... throw some debug message.

With behaviour, I like to use the setHelpText() for immediate visual feedback

def debugMsgs = []
def thisField = getFieldById(getFieldChanged())
def A= getFieldByName("A")
debugMsgs << "A= $A"
debugMsgs << "thisField = $thisField"
debugMsgs << "thisField.value = $thisField.value"


if (thisField.getValue() in (["123"]))
{
debugMsgs << "thisField.value found in (["123"])"
A.setFormValue("568")
debugMsgs << "A.value = $A.value"
}
A.setHelpText(debugMsgs .join('<br>'))

The one thing I would try different  from your code is to remove the parens around ["123"] 

Hunter1428 May 4, 2021

Hey!

I gave your script a go and it's erroring out on the "123". Unfortunately as my groovy knowledge is "just beginner" I was unable to solve it :(

debugMsgs << "thisField.value found in (["123"])"

I did try setting different field type (Text) and all seemed to work great. And I have got single and multi select to work. I can also get the system label field to work, just not custom label fields to work. Are they not supported or a bug?

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 4, 2021

Try it like this:

def debugMsgs = []
def thisField = getFieldById(getFieldChanged())
def A= getFieldByName("A")
debugMsgs << "A= $A"
debugMsgs << "thisField = $thisField"
debugMsgs << "thisField.value = $thisField.value"

def valueMap = ['123':'568']
if (valueMap.containsKey(thisField.value))
{
debugMsgs << "thisField.value found in ${valueMap}"
A.setFormValue("568")
debugMsgs << "A.value = $A.value"
}
A.setHelpText(debugMsgs .join('<br>'))

You'll want to change valueMap. The left side is the value in thisField" that triggers the 'right-side' label. If you have more, you add them comma sparated.

Hunter1428 May 5, 2021

This worked!

It does show the field should be receiving the values. But it still is not adding the label?

A= Form field ID: customfield_11858, value: 568
thisField = Form field ID: customfield_11052, value: 123
thisField.value = 123
thisField.value found in [123:568]
A.value = 568   
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 5, 2021

Not sure what's going on there ... it's working for me.

Here I use the code exactly as is except for def A= getFieldByName("Theme")

The script is put against a random text field ... if I type "123" in my text field, I get this:

2021-05-05 10_47_30-Edit Issue _ PLV-4408 - JIRA Dev.png

Hunter1428 May 6, 2021

Hmmmmm I still cannot get it to work...

I wonder if it's a version issue of Jira/Script runner?

Code:

def debugMsgs = []
def thisField = getFieldById(getFieldChanged())
def A= getFieldByName("Region")
debugMsgs << "A= $A"
debugMsgs << "thisField = $thisField"
debugMsgs << "thisField.value = $thisField.value"

def valueMap = ['123':'USA']
if (valueMap.containsKey(thisField.value))
{
debugMsgs << "thisField.value found in ${valueMap}"
A.setFormValue("USA")
debugMsgs << "A.value = $A.value"
}
A.setHelpText(debugMsgs .join('<br>'))

 Image:

Image.png

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 6, 2021

Perhaps ... I do see a slightly different styling of your Label field than mine.

I'm on Jira 8.13.6 (Software+JSM)

Hunter1428 May 12, 2021

I can only think that it is a version issue.

Gonna do some digging and if I find something will post back.

But for now, thanks for the help. The debugging is super useful!

Suggest an answer

Log in or Sign up to answer