Scripted Field - Need to return value but receive additional info

Cristy Flaherty February 21, 2025

Please bear with me - I am very new to groovy and haven't coded anything in years....

We have two custom dropdown fields (Technical Certainty and Definition Certainty), where users can select “High”, “Medium”, or “Low” :

When either of those values change, we want ScriptRunner to update a “Confidence Level” text field based on the combination of Technical and Definition Certainty. 

Regardless of the values, “Confidence Level” is set to “Undetermined”.  In the script, I returned the values of all three fields and found that it is returning more than just value for the Technical and Definition Certainty fields.  I'm not exactily sure what this is - is it from rest api?  I just want the value shown in bold.

 

The scripted field ran successfully.
"[self:https://******-sandbox.atlassian.net/rest/api/2/customFieldOption/10113, value:Medium, id:10113];
[self:https://******-sandbox.atlassian.net/rest/api/2/customFieldOption/10118, value:High, id:10118];
Undetermined"

Is there something I am missing to pull only the value?  Or is there something I need to do to convert or parse it?  Thoughts?

Thank you for your help!

Here is the script:

def issueKey = issue.key
def TCName = 'Technical Certainty'
def DCName = 'Definition Certainty'
def CLValue = 'Confidence Level'

def TCValue = Issues.getByKey(issueKey).getCustomFieldValue(TCName)
def DCValue = Issues.getByKey(issueKey).getCustomFieldValue(DCName)

def val = [TCValue, DCValue]

switch (val) {
    case {it == ['Low', 'Low']}:
        CLValue = '50%'
        "${TCValue}; ${DCValue}; ${CLValue}";
        break;
    case {it == ['Low', 'Medium']}:
        CLValue = '60%'
        "${TCValue}; ${DCValue}; ${CLValue}";
        break;
    case {it == ['Medium', 'Low']}:
        CLValue = '60%'
        "${TCValue}; ${DCValue}; ${CLValue}";
        break;
    case {it == ['Low', 'High']}:
        CLValue = '70%'
        "${TCValue}; ${DCValue}; ${CLValue}";
        break;
    case {it == ['High', 'Low']}:
        CLValue = '70%'
        "${TCValue}; ${DCValue}; ${CLValue}";
        break;
    case {it == ['Medium', 'Medium']}:
        CLValue = '70%'
        "${TCValue}; ${DCValue}; ${CLValue}";
        break;
    case {it == ['Medium', 'High']}:
        CLValue = '80%'
        "${TCValue}; ${DCValue}; ${CLValue}";
        break;
    case {it == ['High', 'Medium']}:
        CLValue = '80%'
        "${TCValue}; ${DCValue}; ${CLValue}";
        break;
    case {it == ['High', 'High']}:
        CLValue = '90%'
        "${TCValue}; ${DCValue}; ${CLValue}";
        break;
    default:
        CLValue = 'Undetermined'
        "${TCValue}; ${DCValue}; ${CLValue}";
        break;
        }

 

2 answers

Suggest an answer

Log in or Sign up to answer
0 votes
Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 22, 2025

Hi @Cristy Flaherty 

getCustomFieldValue returns more than just the value of the dropdown fields, it returns an JSON string including the id, value and the self calling URL. This is very expected behavior.

You need to modify your script to extract the wanted values.

Could you try the below code? (I've modified more as there were redundant lines)

I hope it helps

 

def issueKey = issue.key
def TCName = 'Technical Certainty'
def DCName = 'Definition Certainty'
def CLField = 'Confidence Level'

def TCValueObj = Issues.getByKey(issueKey).getCustomFieldValue(TCName)
def DCValueObj = Issues.getByKey(issueKey).getCustomFieldValue(DCName)

// Extract only the text values
def TCValue = TCValueObj?.value ?: "Undetermined"
def DCValue = DCValueObj?.value ?: "Undetermined"

// Default Confidence Level
def CLValue = 'Undetermined'

def val = [TCValue, DCValue]

switch (val) {
case ['Low', 'Low']:
CLValue = '50%'
break;
case ['Low', 'Medium'], ['Medium', 'Low']:
CLValue = '60%'
break;
case ['Low', 'High'], ['High', 'Low']:
CLValue = '70%'
break;
case ['Medium', 'Medium']:
CLValue = '70%'
break;
case ['Medium', 'High'], ['High', 'Medium']:
CLValue = '80%'
break;
case ['High', 'High']:
CLValue = '90%'
break;
}

return "${TCValue}; ${DCValue}; ${CLValue}"

 

Cristy Flaherty February 24, 2025

Thank you - this is wonderful!  I am having an issue with the switch statement, though.  It does not like having two options.    I get the following error:

Script_aee969a37c7757b384a20479bab0ff7e.groovy: 21: break statement is only allowed inside loops or switches @ line 21, column 9.

Here are the first two switch cases - the second is what causes the error.   (I used the code you provided)

switch (val) {
    case ['Low', 'Low']:
        CLValue = '50%'
        break;
    case ['Low', 'Medium'], ['Medium', 'Low']:
        CLValue = '60%'
        break;

Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 25, 2025

Hmm, that's weird, seems like Groovy interpreter does not accept break inside the switch. Anyway, I'll rewrite the script without break statements. 

def issueKey = issue.key
def TCName = 'Technical Certainty'
def DCName = 'Definition Certainty'
def CLField = 'Confidence Level'

def TCValueObj = Issues.getByKey(issueKey).getCustomFieldValue(TCName)
def DCValueObj = Issues.getByKey(issueKey).getCustomFieldValue(DCName)

// Extract only the text values
def TCValue = TCValueObj?.value ?: "Undetermined"
def DCValue = DCValueObj?.value ?: "Undetermined"

// Default Confidence Level
def CLValue = 'Undetermined'

def confidenceMap = [
['Low', 'Low'] : '50%',
['Low', 'Medium'] : '60%',
['Medium', 'Low'] : '60%',
['Low', 'High'] : '70%',
['High', 'Low'] : '70%',
['Medium', 'Medium'] : '70%',
['Medium', 'High'] : '80%',
['High', 'Medium'] : '80%',
['High', 'High'] : '90%'
]

CLValue = confidenceMap.get([TCValue, DCValue], 'Undetermined')

return "${TCValue}; ${DCValue}; ${CLValue}"

 

Cristy Flaherty February 26, 2025

@Tuncay Senturk - That runs without errors and returns the expected values.  Unfortunately, it doesn't update the field in the issue.  I even tried running it in the Script Console, thinking "Test Issue" would test the code but not make the change.  It doesn't even change "Confidence Level" to the default value "Undetermined" - it just shows "None".

Cristy Flaherty February 26, 2025

Nevermind!  I figured out what I was doing wrong.  I created a custom field thinking the scripted field would populate there.  I see now that I had to create the scripted field through scriptrunner THEN add that field to the issue.  (Thank you @Mohamed Benziane.  I should have watched the video you sent earlier).

Thank you both for all your help!

0 votes
Mohamed Benziane
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 22, 2025

Hi,

welcome to the community

In cloud if you want to get the data from a field you need to call a rest api with scritprunner. Below some example from the scriptrunner doc. The first one should help you

https://docs.adaptavist.com/sr4jc/latest/features/scripted-fields/example-scripted-fields

Cristy Flaherty February 24, 2025

Thank you!

DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events