The Atlassian Community Forums are currently in read-only mode. We will be relaunching on a new platform on September 22 (read more here). We apologize for the extended downtime. For concerns or questions, please email communitymanagers@atlassian.com. See you on the other side, on the new Atlassian Community Forums! :)

×

Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

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

Hunter1428
Contributor
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

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

0 votes
PD Sheehan
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 Champions.
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
Contributor
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?

PD Sheehan
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 Champions.
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
Contributor
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   
PD Sheehan
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 Champions.
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
Contributor
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

PD Sheehan
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 Champions.
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
Contributor
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!