Forums

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

Custom panel using Scriptrunner with coloured scripted field

Mark Fallaize December 24, 2020

Hi all,

In Jira Server using Scriptrunner, I want to create a custom panel containing risk rating details. So far, I have got the Inherent Risk Rating panel working fine, but when I duplicated the code (with minor tweaks) for the Residual Risk Rating panel, the risk exposure scripted field does not get coloured. I've compared the codes for the 2 panels and cannot see why one works and the other doesn't. The code is a mix of other users queries, and I'm new to Groovy.

It seems that to display scripted fields in a panel with colour, the lazy loaded option can't be used (whatever this does), and the switch command needs a slightly different format. So instead of "switch (cField3?.value) {", it needs to be "switch (cField3) {". And this worked for the Risk Inherent Exposure field. 

Here's what the panels look like now, where you can see the Risk Residual Exposure rating is not coloured:

risk ratings panel.PNG

The exposure fields are calculated based on the impact/likelihood fields.

Here is the code for the Residual Risk Rating panel:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.RendererManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption

def customFieldManager = ComponentAccessor.customFieldManager
def issue = context.issue as Issue
String customfield1 = "customfield_11002"
String customfield2 = "customfield_11003"
String scriptedfield = "customfield_13802"

def rendererManager = ComponentAccessor.getComponent(RendererManager)
def fieldLayoutItem1 = ComponentAccessor.getFieldLayoutManager().getFieldLayout(issue).getFieldLayoutItem(customfield1)
def renderer = rendererManager.getRendererForField(fieldLayoutItem1)

def fieldLayoutItem3 = ComponentAccessor.getFieldLayoutManager().getFieldLayout(issue).getFieldLayoutItem(scriptedfield)
def renderer3 = rendererManager.getRendererForField(fieldLayoutItem3)

//Get custom fields

final customFieldName1 = "Risk Residual Impact"
final customFieldName2 = "Risk Residual Likelihood"
final scriptedFieldName = "Risk Residual Exposure"

def customField1 = customFieldManager.getCustomFieldObjects(issue).findByName(customFieldName1)
def customField2 = customFieldManager.getCustomFieldObjects(issue).findByName(customFieldName2)
def scriptedField = customFieldManager.getCustomFieldObjects(issue).findByName(scriptedFieldName)

def cf1 = customFieldManager.getCustomFieldObject(customfield1)
def cfname1 = cf1.getFieldName()
def cf2 = customFieldManager.getCustomFieldObject(customfield2)
def cfname2 = cf2.getFieldName()
def cf3 = customFieldManager.getCustomFieldObject(scriptedfield)
def cfname3 = cf3.getFieldName()

def cField1 = issue.getCustomFieldValue(customField1) as LazyLoadedOption
def cField2 = issue.getCustomFieldValue(customField2) as LazyLoadedOption
def cField3 = issue.getCustomFieldValue(scriptedField)

String value1 = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject(customfield1))
String value2 = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject(customfield2))
String value3 = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject(scriptedfield))

//Add Risk Residual Impact
writer.write(cfname1 + ": " + "<div id='" + customfield1 + "-val' class='field-ignore-highlight editable-field inactive' title='Click to edit'>" +
"<div class='user-content-block'>" +
renderer.render(value1, issue.getIssueRenderContext()) +
"</div>" +
"</div>" )

//Add Risk Residual Likelihood
writer.write(cfname2 + ": " + "<div id='" + customfield2 + "-val' class='field-ignore-highlight editable-field inactive' title='Click to edit'>" +
"<div class='user-content-block'>" +
renderer.render(value2, issue.getIssueRenderContext()) +
"</div>" +
"</div>" )

//Add Risk Residual Exposure
writer.write(cfname3 + ": " + "<div id='" + scriptedfield + "-val' class='field-ignore-highlight editable-field inactive' title='Click to edit'>" +
"<div class='user-content-block'>" +
renderer3.render(value3, issue.getIssueRenderContext()) +
"</div>" +
"</div>" )

//Assign background colour depending on Risk Residual Impact
switch (cField1?.value) {
case "Critical":
writer.write("<script>var css = '#customfield_11002-val { color: white; font-weight: bold; background: red; border-radius: 5px; display: flex; justify-content: center; flex-wrap: wrap; }', head = document.head || document.getElementsByTagName('head')[0], style = document.createElement('style');head.appendChild(style);style.appendChild(document.createTextNode(css)); </script>")
break
case "High":
writer.write("<script>var css = '#customfield_11002-val { color: black; font-weight: bold; background: Orange; border-radius: 5px; display: flex; justify-content: center; flex-wrap: wrap; }', head = document.head || document.getElementsByTagName('head')[0], style = document.createElement('style');head.appendChild(style);style.appendChild(document.createTextNode(css)); </script>")
break
case "Medium":
writer.write("<script>var css = '#customfield_11002-val { color: black; font-weight: bold; background: Yellow; border-radius: 5px; display: flex; justify-content: center; flex-wrap: wrap; }', head = document.head || document.getElementsByTagName('head')[0], style = document.createElement('style');head.appendChild(style);style.appendChild(document.createTextNode(css)); </script>")
break
case "Low":
writer.write("<script>var css = '#customfield_11002-val { color: white; font-weight: bold; background: Green; border-radius: 5px; display: flex; justify-content: center; flex-wrap: wrap; }', head = document.head || document.getElementsByTagName('head')[0], style = document.createElement('style');head.appendChild(style);style.appendChild(document.createTextNode(css)); </script>")
break
case "Very Low":
writer.write("<script>var css = '#customfield_11002-val { color: black; font-weight: bold; background: #b3ff66; border-radius: 5px; display: flex; justify-content: center; flex-wrap: wrap; }', head = document.head || document.getElementsByTagName('head')[0], style = document.createElement('style');head.appendChild(style);style.appendChild(document.createTextNode(css)); </script>")
break
}

//Assign background colour depending on Risk Residual Likelihood
switch (cField2?.value) {
case "Very Likely (Once a month)":
writer.write("<script>var css = '#customfield_11003-val { color: white; font-weight: bold; background: red; border-radius: 5px; display: flex; justify-content: center; flex-wrap: wrap; }', head = document.head || document.getElementsByTagName('head')[0], style = document.createElement('style');head.appendChild(style);style.appendChild(document.createTextNode(css)); </script>")
break
case "Likely (Once a quarter)":
writer.write("<script>var css = '#customfield_11003-val { color: black; font-weight: bold; background: Orange; border-radius: 5px; display: flex; justify-content: center; flex-wrap: wrap; }', head = document.head || document.getElementsByTagName('head')[0], style = document.createElement('style');head.appendChild(style);style.appendChild(document.createTextNode(css)); </script>")
break
case "Possible (Once a year)":
writer.write("<script>var css = '#customfield_11003-val { color: black; font-weight: bold; background: Yellow; border-radius: 5px; display: flex; justify-content: center; flex-wrap: wrap; }', head = document.head || document.getElementsByTagName('head')[0], style = document.createElement('style');head.appendChild(style);style.appendChild(document.createTextNode(css)); </script>")
break
case "Remote (Once in 3 Years)":
writer.write("<script>var css = '#customfield_11003-val { color: white; font-weight: bold; background: Green; border-radius: 5px; display: flex; justify-content: center; flex-wrap: wrap; }', head = document.head || document.getElementsByTagName('head')[0], style = document.createElement('style');head.appendChild(style);style.appendChild(document.createTextNode(css)); </script>")
break
case "Very Remote (Once in 5 Years)":
writer.write("<script>var css = '#customfield_11003-val { color: black; font-weight: bold; background: #b3ff66; border-radius: 5px; display: flex; justify-content: center; flex-wrap: wrap; }', head = document.head || document.getElementsByTagName('head')[0], style = document.createElement('style');head.appendChild(style);style.appendChild(document.createTextNode(css)); </script>")
break
}

//Assign background colour depending on Risk Residual Exposure
switch (cField3) {
case "Critical":
writer.write("<script>var css = '#customfield_13802-val { color: white; font-weight: bold; background: red; border-radius: 5px; display: flex; justify-content: center; flex-wrap: wrap; }', head = document.head || document.getElementsByTagName('head')[0], style = document.createElement('style');head.appendChild(style);style.appendChild(document.createTextNode(css)); </script>")
break
case "High":
writer.write("<script>var css = '#customfield_13802-val { color: black; font-weight: bold; background: Orange; border-radius: 5px; display: flex; justify-content: center; flex-wrap: wrap; }', head = document.head || document.getElementsByTagName('head')[0], style = document.createElement('style');head.appendChild(style);style.appendChild(document.createTextNode(css)); </script>")
break
case "Medium":
writer.write("<script>var css = '#customfield_13802-val { color: black; font-weight: bold; background: Yellow; border-radius: 5px; display: flex; justify-content: center; flex-wrap: wrap; }', head = document.head || document.getElementsByTagName('head')[0], style = document.createElement('style');head.appendChild(style);style.appendChild(document.createTextNode(css)); </script>")
break
case "Low":
writer.write("<script>var css = '#customfield_13802-val { color: white; font-weight: bold; background: Green; border-radius: 5px; display: flex; justify-content: center; flex-wrap: wrap; }', head = document.head || document.getElementsByTagName('head')[0], style = document.createElement('style');head.appendChild(style);style.appendChild(document.createTextNode(css)); </script>")
break
}

Any ideas much appreciated!

Regards

Mark

1 answer

1 accepted

0 votes
Answer accepted
Mark Fallaize December 29, 2020

The answer is, 2 custom fields had the same name. I renamed one, and it works as expected. I hope the above code helps others.

Suggest an answer

Log in or Sign up to answer