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

Access to JIRA variables in Velocity Template file

Quentin Genies April 13, 2013

Hello.

I am quite new to JIRA development and after days of searching the forums and JIRA Documentation, I have been unable to find any help.

What I want to achieve is retrieving information from my JIRA instance on "Create Issue" screen (especially User Properties and User Role on a Project) in my Velocity Template file (to store them in a hidden text HTML tag), and being able to use them through JavaScript. All of this to make a JavaScript run through a CustomField plugin described below in xml.

<customfield-type name="Working Custom Field JS" i18n-name-key="working-custom-field-js.name" key="working-custom-field-js" class="com.atlassian.tutorial2.jira.customfields.WorkingCustomFieldJS">
    <description key="working-custom-field-js.description">Working Custom Field</description>
	<resource type="velocity" name="edit" location="templates/plugins/fields/edit/edit-hiddenjs.vm"/>
  </customfield-type>

I only succeeded in getting Project attributes.

Is there anywhere I can find which variables are available to Velocity Templates?

Am I doing it the right way?

#disable_html_escaping() TODO REENABLE
#customControlHeader ($action $customField.id $customField.name $fieldLayoutItem.required $displayParameters $auiparams)

<input class="text" type="hidden" id="currentProject" value="$issue.getProject().getString("name")" />
	## or
	##<input class="text" type="hidden" id="currentProject" value="$!issue.projectObject.name" />

#customControlFooter ($action $customField.id $fieldLayoutItem.fieldDescription $displayParameters $auiparams)

I am using JIRA 5.1 by the way.

Thank you.

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

8 votes
Answer accepted
Quentin Genies April 22, 2013

Answered by Nic Brough.

0 votes
RambanamP
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.
April 14, 2013
Quentin Genies April 14, 2013

Thank you for the answer but I already have visited this page. Tried a few combinations like this :

<input class="text" type="hidden" id="userRole" value="$remoteUser.getDisplayName" />
<input class="text" type="hidden" id="userRole" value="$remoteUser.getDisplayName()" />
<input class="text" type="hidden" id="userRole" value="$remoteUser.getFullName()" />

Which are in fact methods from the Java JIRA API. Still, it does not get interpreted. And by the way, that link is for JIRA 4.2, I am using 5.1.

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 14, 2013

The second line looks correct to me - $remoteUser.getDisplayName() - I've used

I tend to use {} inside velocity when there are strings and " marks knocking around, to make it clear it's a velocity command. I don't really think it's that, but try "${removeUser.getDisplayName()}" to rule it out.

But, I think the actual problem is going to be that remoteUser is not being exposed to your code. You originally asked about what variables are available in a velocity context, which is the right idea. But the answer is "it depends". A velocity context can have hundreds of variables, or next-to-none. It depends on the code behind it. You get a different set of variables (and objects) exposed, depending on what the template is doing and where it's used.

So, the answer might be that your velocity is fine, but your java behind it is not making a user object available.

Quentin Genies April 14, 2013

Ok now this is some clear answer, I wasn't sure about that as nothing is really stating it clearly in tutorials!

Is too much to ask for a hint about how to invoke a user-like object in my Java CustomField class (the API is so huge I can't think clearly about what to actually use...)?

Here is what's behind :

package com.atlassian.tutorial2.jira.customfields;

import org.slf4j.Logger;
...

public class WorkingCustomFieldJS extends GenericTextCFType {
    private static final Logger log = LoggerFactory.getLogger(WorkingCustomFieldJS.class);

    public WorkingCustomFieldJS(CustomFieldValuePersister customFieldValuePersister, GenericConfigManager genericConfigManager) {
    	
    	super(customFieldValuePersister, genericConfigManager);
    	
    }
    
    @Override
    public Map<String, Object> getVelocityParameters(final Issue issue,
                                                     final CustomField field,
                                                     final FieldLayoutItem fieldLayoutItem) {
        final Map<String, Object> map = super.getVelocityParameters(issue, field, fieldLayoutItem);

        if (issue == null) {
            return map;
        }

         FieldConfig fieldConfig = field.getRelevantConfig(issue);
         //add what you need to the map here

        return map;
    }
}

Thank you!

Quentin Genies April 14, 2013

That's all I needed to know, lots of thanks!

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 14, 2013

Yup, you're nearly there. In your java, where it has the "add what you need" comment, that's (not very clearly for new developers) explaining that you need to put stuff into the "map".

Your Velocity will be handed the contents of the "map" when it's asked to return the html that eventually goes to the user, so you need to add the objects you want to access into the map, then use them in your velocity.

//me: makes industrious rummaging noises in his own code...

Map.put("remoteUser", aUserObject);

Obviously, you'll need to write a spot of java to populate aUserObject with a user. The same principles apply for other objects too, numbers, strings, even lists of other objects (although for lists and arrays, you need to tell velocity to iterate through them)

TAGS
AUG Leaders

Atlassian Community Events