Scripted Field HTML and Dashboard Gadgets

Alex Christensen
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 11, 2015

I've created a script that looks like this:

String customField = getCustomFieldValue("custom field")
Double index = Double.parseDouble(customField)

if (index <= 1) {
    return "<div style='color:green;'><b>Low</b></div>"
    }
else if (index > 1 && index < 2) {
    return "<div style='color:green;'><b>Normal</b></div>"
    }
else if (index >= 2 && index < 4) {
    return "<div style='color:#FFBF00;'><b>High</b></div>"
    }
else if (index > 4) {
    return "<div style='color:red;'><b>MAX</b></div>"
    }
else {
    return "<div style='color:red;'><b>ERROR</b></div>"
    }

This script works great and exactly as I've intended. The correct formatting displays when I show the column on a View screen, in a column in the Issue Navigator, in an exported Excel file, and in a column within a gadget.

However, I'd like to be able to use the values in this field as the basis for a gadget, such as the basis for an Issue Statistics gadget. However, when doing this, the value in the gadget isn't returned as the formatted text (for example, green bolded "Low"), but appears as a string of HTML code. Is there a way to only return the text without the full HTML code when basing a gadget on this field?

Thank you in advance for your help here!

2 answers

1 accepted

0 votes
Answer accepted
Steven F Behnke
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.
September 11, 2015

This is untested. I haven't even inputted it into a system, I'm just trying to give you an idea of what I mean. You should separate the styling from the values.

Script Field

String customField = getCustomFieldValue("custom field")
Double index = Double.parseDouble(customField)
 
if (index <= 1) {
    return "Low"
    }
else if (index > 1 && index < 2) {
    return "Normal"
    }
else if (index >= 2 && index < 4) {
    return "High"
    }
else if (index > 4) {
    return "MAX"
    }
else {
    return "ERROR"
    }

Custom Template

#if ($value = "Low")
<div style='color:green;'><b>$value</b></div>
#elseif ($value = "Normal")
<div style='color:green;'><b>$value</b></div>
#elseif ($value = "High")
<div style='color:#FFBF00;'><b>$value</b></div>
#elseif ($value="Max")
<div style='color:red;'><b>$value</b></div>
#else ($value="Error")
<div style='color:red;'><b>$value</b></div>
#end
Alex Christensen
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 14, 2015

Thanks for your answer, Steven. This should help point me in the right direction. Appreciate your help!

Robert Wojtowicz March 20, 2018

Doesn't appear to work anymore, the formatted HTML is encased in a string when used as a custom field, so you see the raw HTML on the issue screen. :/

0 votes
Steven F Behnke
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.
September 11, 2015

You should not be using raw HTML to format the field value with a DIV and styling. You should be exploiting the 'Template' function of your scripted field to achieve the look and feel, and the field-value should be NOTHING except the value.

 https://scriptrunner.adaptavist.com/latest/jira/scripted-fields.html#_custom_templates

Suggest an answer

Log in or Sign up to answer