Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Get customField of ParentIssue in Velocity email templates

Eric Thn May 22, 2013

I've been trying to get my hands on the parent issue customfields values, to no avail.

Parent issue details show up, but no custom fields info...
(btw the custom field exists and was populated for the tests)

here is my code for the <tt>templates/email/subject/issuecreated.vm</tt> template...

<tt>#disable_html_escaping()</tt>
<tt>#if ($issue.isSubTask())</tt>
<tt>#set ($issueParent = $issue.getParentObject())</tt>
<tt>#set( $parentFieldValue = $issueParent.getCustomFieldValue( "10000" ) )</tt>
<tt>#if ($parentFieldValue)</tt>
<tt>[$parentFieldValue] ($issue.key) $issue.summary <($issueParent.getKey()) $issueParent.getSummary()</tt>
<tt>#else</tt>
<tt>($issue.key) $issue.summary < $issueParent.getSummary()</tt>
<tt>#end</tt>
<tt>#else</tt>
<tt>($issue.key) $issue.summary</tt>
<tt>#end</tt>

Any help or information on the availability of parent issue custom fields in the velocity email context would be much apreciated.

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
Kavian Moradhassel May 23, 2013

Eric, the JIRA API treats "customfield_10000" as the custom field ID, not the pure numeric value stored in the database. So the second one is more correct.

However, it isn't completely correct, as you cannot get the CustomField object from the issue object. You need to use the CustomFieldManager, which is provided in velocity as $customFieldManager, as described on this page:

https://developer.atlassian.com/display/JIRADEV/Velocity%20Context%20for%20Email%20Templates

So your code would probably need to look something like this:

#set( $parentField = $customFieldManager.getCustomFieldObject("customfield_10000") )
#set( $parentValue = false )

#if ( $parentField )
#set( $parentValue = $issueParent.getCustomFieldValue( $parentField ) )
#end

#if ( $parentValue )
...
#end

Note that the initial setting of $parentValue to false is necessary, as Velocity will not override a previously-set variable to null, so setting to false first avoids having field values bleed from one notification to the next.

Also note that $parentValue will be an object of a type specific to the custom field type, i.e. it could be a Version or a User, or some other type. To display the value, your best bet for the widest coverage would be to use $parentValue.toString().

Eric Thn May 23, 2013

Final code : (works like a charm, thanks)

#disable_html_escaping()
#set( $parentField = $customFieldManager.getCustomFieldObject("customfield_10000") )
#set( $parentFieldValue = false )
#if ($issue.isSubTask())
#set( $issueParent = $issue.getParentObject() )
#if ( $parentField )
#set( $parentFieldValue = $issueParent.getCustomFieldValue( $parentField ) )
#end
#if ( $parentFieldValue )
[$parentFieldValue] ($issue.key) $issue.summary &lt;($issueParent.getKey()) $issueParent.getSummary()
#else
($issue.key) $issue.summary &lt;($issueParent.getKey()) $issueParent.getSummary()
#end
#else
#if ( $parentField )
#set( $parentFieldValue = $issue.getCustomFieldValue( $parentField ) )
#end
#if ( $parentFieldValue )
[$parentFieldValue] ($issue.key) $issue.summary
#else
($issue.key) $issue.summary
#end
#end

Eric Thn May 23, 2013

Final code : (works like a charm, thanks)

#disable_html_escaping()
#set( $parentField = $customFieldManager.getCustomFieldObject("customfield_10000") )
#set( $parentFieldValue = false )
#if ($issue.isSubTask())
#set( $issueParent = $issue.getParentObject() )
#if ( $parentField )
#set( $parentFieldValue = $issueParent.getCustomFieldValue( $parentField ) )
#end
#if ( $parentFieldValue )
[$parentFieldValue] ($issue.key) $issue.summary &lt;($issueParent.getKey()) $issueParent.getSummary()
#else
($issue.key) $issue.summary &lt;($issueParent.getKey()) $issueParent.getSummary()
#end
#else
#if ( $parentField )
#set( $parentFieldValue = $issue.getCustomFieldValue( $parentField ) )
#end
#if ( $parentFieldValue )
[$parentFieldValue] ($issue.key) $issue.summary
#else
($issue.key) $issue.summary
#end
#end

1 vote
Jobin Kuruvilla [Adaptavist]
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.
May 22, 2013

You need to pass a custom field object to getCustomFieldValue method, not just the id (10000).

0 votes
Eric Thn May 22, 2013

Since i've already tried multiple approaches, an example would be welcome.

#set( $parentField = $issueParent.getCustomField( "10608" ) )
#set( $parentValue = $issueParent.getCustomFieldValue( $parentField ) )

or

#set( $parentField = $issueParent.getCustomField( "customfield_10608" ) )
#set( $parentValue = $issueParent.getCustomFieldValue( $parentField ) )

TAGS
AUG Leaders

Atlassian Community Events