Dynamic templates - Examples

Examples of using variables that are available for use in dynamic templates.

Java doc for Message field.

You can see the official user guide for velocity here.

Common

How to set the value of variable?

#set( $foo = "Velocity" )
Hello $foo World!

Conditionals

#set( $foo = "Velocity" )

Hello 
#if( $foo )
    <strong>Velocity!</strong>
#end
World!

Loops

<ul>
#foreach( $product in $allProducts )
    <li>$product</li>
#end
</ul>

Comments

## This is a single line comment.  

#*
  Thus begins a multi-line comment. Online visitors won't
  see this text because the Velocity Templating Engine will
  ignore it.
*#   

Getting data from the screens

How to use $form? Get raw data from issue screen.

$form.summary <br>
$form.customfield_10400

How to use $formIssue? Get valid data from the issue screen.

see Java doc $formIssue

$formIssue.summary <br>
$formIssue.priority.name <br>
#if($formIssue.assignee)
    $formIssue.assignee.displayName<br>
#else
    Unassigned<br>
#end

How to use $cfValues? How to get the value of a custom field for an $issue?

see Java doc $cfValues

$cfValues.get(10100)<br>
$cfValues.get("customfield_10100")<br>
$cfValues.get("Custom field name")<br>

$cfValues.getOrDefault(10100, "Default value")<br>
$cfValues.getOrDefault("customfield_10100", 123)<br>
$cfValues.getOrDefault("Custom field name", $issue.assignee)<br>

##use for get value for linked issue
$cfValues.get($linkedIssue, 10100)<br>
$cfValues.getOrDefault($linkedIssue, "customfield_10100", "Default value")<br>
$cfValues.getOrDefault($linkedIssue, "Custom field name", 123)<br>

How to get the value of a custom field for an $formIssue?

$cfValues.getFromForm(10100)<br>
$cfValues.getFromForm("customfield_10100")<br>
$cfValues.getFromForm("Custom field name")<br>

$cfValues.getFromFormOrDefault(10100, "Default value")<br>
$cfValues.getFromFormOrDefault("customfield_10100", 123)<br>
$cfValues.getFromFormOrDefault("Custom field name", $issue.assignee)<br>

How to use $fieldDisplayConfig? How to change the display of a message?

see Java doc $fieldDisplayConfig

$fieldDisplayConfig.setAsFlag(true) ## true, false
$fieldDisplayConfig.setMessageType("info") ## "info", "error", "success", "warning", "change", "SIMPLE_VIEW"
$fieldDisplayConfig.setTitle("New title") 
$fieldDisplayConfig.setShowForFieldId("summary") ## "customfield_10100"
$fieldDisplayConfig.setInsert("before") ## "append", "prepend", "before", "after" 
$fieldDisplayConfig.setHidden(true)

How to hide a message?

see Java doc $fieldDisplayConfig

#set ($optionCustomFieldValue = $cfValues.getFromForm("Option custom field name"))
#if($optionCustomFieldValue.value == "option 1" )
    $fieldDisplayConfig.setTitle("New title for option 1")
    Body text for option 1<br> 
#elseif($optionCustomFieldValue.value == "option 2" )
    #set ($titleForOption2 = "New title for " + $optionCustomFieldValue.value)
       $fieldDisplayConfig.setTitle($titleForOption2)
       Body text for option 2<br>
#else
    $fieldDisplayConfig.setHidden(true)##hide
#end

How to change message colour?

see Java doc $fieldDisplayConfig

dynamic template random message colors.gif

$fieldDisplayConfig.setMessageType("custom") ##require for custom colors
#set($r = $mathTool.random(0,255))
#set($g = $mathTool.random(0,255))
#set($b = $mathTool.random(0,255))
$fieldDisplayConfig.setBackgroundColor("rgb($r,$g,$b)") ## color as hex "#FFFFFF", "rgb(255,255,255)", "rgba(255,255,255, 1)"
#set($r = $mathTool.random(0,255))
#set($g = $mathTool.random(0,255))
#set($b = $mathTool.random(0,255))
$fieldDisplayConfig.setTextColor("rgb($r,$g,$b)")
#set($r = $mathTool.random(0,255))
#set($g = $mathTool.random(0,255))
#set($b = $mathTool.random(0,255))
$fieldDisplayConfig.setIconColor("rgb($r,$g,$b)")


$issue.key: $issue.summary

How to use $context, $transitionId and $transitionName?

#if($context == "TRANSITION")
	is transitionId 31 ? #if($transitionId == 31) Yes #else No #end <br>
	is transitionName "In Progress" ? #if($transitionName == "In Progress") Yes #else No #end<br>
#end

How to use and check linked issues?

see Java doc $links

#set ($blockedIssues = $links.getOutwardIssues("blocked"))
#if($blockedIssues.size() > 0)
    #foreach($blockedIssue in $blockedIssues)
        $blockedIssue.status.name $cfValues.getOrDefault($blockedIssue, "developer custom field", "hasn't developer")<br>
    #end
#end

Show message for “description” and “assignee” depending on the priority.

dynamic template demo.gif

#if($formIssue.priority.name == "Blocker")
	$fieldDisplayConfig.setMessageType("error")
	#if(!$formIssue.description || $formIssue.description.isEmpty())
		$fieldDisplayConfig.setShowForFieldId("description")
		State the reason for the Blocker priority.
	#elseif(!$formIssue.assignee)
		$fieldDisplayConfig.setShowForFieldId("assignee")
		Indicate the responsible employee
	#else
	  $fieldDisplayConfig.setHidden(true)
	#end
#elseif($formIssue.priority.name == "High")
	$fieldDisplayConfig.setMessageType("warning")
	#if($formIssue.description.isEmpty())
		$fieldDisplayConfig.setShowForFieldId("description")
		State the reason for the High priority.
	#else
	  	  $fieldDisplayConfig.setHidden(true)
	#end
#elseif($formIssue.priority.name == "Medium" || $formIssue.priority.name == "Low")
	$fieldDisplayConfig.setHidden(true)
#end

How to use JQL in message? How to use $jqlService?

see Java doc $jqlService

$jqlService.getIssuesByJQL("priority = $formIssue.priority.name ORDER BY Key DESC", 10)

You can use current issue in jql for conditions:

#if($jqlService.getIssueCountByJQL("key = $issue.key and updated < startOfDay(-2)") > 0)
    The problem has not been updated for more than two days.
#end

How to show fields for issues by JQL in message? How to use $issueFieldRender?

see Java doc $issueFieldRender

$issueFieldRender.getAsTableHtml( 
	$jqlService.getIssuesByJQL("priority = $formIssue.priority.name ORDER BY Key DESC", 5), 
	"issue key", "Priority", "Assignee", "customfield_10110", "Product categorization", "10110"
	)

Rest API
Custom colors and HTML