Setting a Custom Field Value using 'Custom Field Defaults' and Velocity

since 1.7.7

Summary

It is possible to script (using Velocity) a dynamic custom field value to add to issues.  The script is parsed, and whatever value is present in the template after rendering, will be the value applied to the custom field.

For more information on using Velocity see the User Guide here: http://velocity.apache.org/engine/devel/user-guide.html





This guide refers to the legacy custom field default system introduced in version 1.7.7, which is found at the top profile level.  Information on using the new project mapping-level system can be found here: Use Custom Field Defaults.



Example - Setting a Date field 1 day in the future

Create a new Custom Field Default

When the Field Value type is set to Velocity Scripted Value, the "Velocity Context" is expandable, showing the contents of the JEMH Velocity Context, with hyperlinks to online Javadoc.

Showing the velocity context

The icon pops up the velocity context available, including (for now) some static values that would be supplied during real processing

Example content:

since 2.4.21

context variable

Data Type

Description

context variable

Data Type

Description

message

javax.mail.Message

the raw message object

subject

java.lang.String

the Subject text

$jemhUtils.getCatchEmailAddress().getAddress()

java.lang.String

The Mailbox catchEmailAddress

toAddresses

javax.mail.InternetAddress[]

the to: addressees

ccAddresses

javax.mail.InternetAddress[]

the cc: addressees

bccAddresses

javax.mail.InternetAddress[]

the bcc: addressees (don't expect many here!)

allAddresses

javax.mail.InternetAddress[]

all addressees

body

java.lang.String

the post processed (html > markup) content – since JEMH 2.7.36 for Jira 7.10.x

jemhUtils

http://ppl-docs.s3-website-us-east-1.amazonaws.com/JEMH/4.1.0/com/javahollic/jira/emh/api/IJEMHVelocityContextUtils.html

JEMH Helper class

 

Add the Velocity code

Example: Email Address arrays:

#foreach ($aCc in $ccAddresses) $aCc.getAddress() : #end

Dates

$longVal is defined as a variable, (with the value of the current time in milliseconds, plus 24(hours)*60(minutes)*60(seconds)*1000(millis) = 8640000), then the $dateFormatter is used with a specific style, based on the date generated from $jemhDateUtils.createDate:

#set($longVal = $currentMillis.longValue() + 86400000 ) $dateFormatter.withStyle($dateTimeStyle.DATE).format($jemhDateUtils.createDate($longVal))

Preview the output

Press the blue preview button to render the Velocity code in the Preview window.

After Submitting the form, the Custom Field Defaults section view will show the rendered Velocity values as applicable (here showing the next days date, 15/Oct/16 in a valid format):

Now, issues created, will have the Custom Field Test Date set with a dynamically generated value of 29/Mar/15, which is correctly parsed by JIRA and results in issues being created with the appropriate value: