Forums

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

How to get a custom field's rendered view-HTML with Java API ?

Johannes Heger (CD)
Contributor
March 20, 2019

Hello,

in a Jira add-on, I like to get the rendered view-HTML of a custom field in an easy way in Java. I have the issue key, the custom field object, and the user name. The solution sould do all the inconvenient stuff like collecting parameters for e. g. CustomFieldRenderer.

The custom field's value isn't sufficient, as that is only a primary key of something another add-on renders.

Best regards, Johannes

2 answers

4 votes
Ivana Navratilova
Contributor
July 2, 2019

By sheer coincidence I think I have found a solution to this problem, so I am leaving it here just in case someone stumbles upon it later.

There is a method in CustomField getViewHtml which apparently does exactly what you want it to do - renders a customfield value into HTML. The problem is it takes some arguments I dont really understand and dont know where and how to obtain. However, I have managed to work around it and it seems to be working fine (tested on some "normal" customfields like cascade select and multiselect).

You basically only need two things - the customfield id (the "customfield_XXXX" code) and the issue from which you want to take the value. The getViewHtml takes three parameters - the issue, a field layout item (I only have a vague idea of what that represents exactly and the documentation isnt exactly complete on this topic) and an action. Now I assume the action is supposed to be something like the Edit/View action on the issue, but how to obtain it in a code goes way beyond me. I have tried to simply create an empty instance of JiraWebActionSupport and by some miracle it actually works.

The following code returns an HTML code for rendering a value of the given customfield from the given issue.

IssueManager issueManager = ComponentAccessor.getIssueManager();
Issue issue = issueManager.getIssueByCurrentKey(issueKey);

FieldLayoutManager fieldLayoutManager = ComponentAccessor.getFieldLayoutManager();
FieldLayout fieldLayout = fieldLayoutManager.getFieldLayout(issue);
FieldLayoutItem fieldLayoutItem = fieldLayout.getFieldLayoutItem(customFieldId);

Action action = new JiraWebActionSupport();

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField customField = customFieldManager.getCustomFieldObject(customFieldId);

String rendered = customField.getViewHtml(fieldLayoutItem, action, issue);
0 votes
Ivana Navratilova
Contributor
July 1, 2019

Have you had any luck solving this issue? I have ran into this problem several times and havent found any convenient solutions to render custom field values in a nice way, since each type is rendered differently, not even talking about additional customfields added by plugins.

Johannes Heger (CD)
Contributor
July 1, 2019

Ivana, unfortunetly I didn't find a solution (and I'm no longer on it).

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events