Scripted Field HTML and Dashboard Gadgets

Alex Christensen Community Champion Sep 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 vote
Steven Behnke Community Champion Sep 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 Champion Sep 14, 2015

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

0 vote
Steven Behnke Community Champion Sep 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 Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,227 views 14 19
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot