Programmatically create a customfield and save value into it

In a Webwork action class, in the submit action, I want to programmatically create a CustomField and save some value in it. I have the value which I get as part of a post. Now I want to save it so that I can access the value of this custom filed where I have access to issue. I dont display this filed on the page. Here is the code I tried but its not saving the value.

String videoLink = (String) parametes.get("videos");

CustomFieldType fieldType = customFieldManager.getCustomFieldType("com.atlassian.jira.plugin.system.customfieldtypes:textfield");
videoField = customFieldManager.createCustomField("videos", "Video link", fieldType , null, null, null);
videoField.createValue(getIssue(), videoLink);
videoField.store();

What am I doing wrong here?

2 answers

1 accepted

Accepted Answer
1 vote

According to your actual requirements it sounds you are integrating Jira with a third party application. Supposing you are developing for Jira5 consider changing from customfield development to "JIRA Remote Issue Links" as it is explained at https://developer.atlassian.com/display/JIRADEV/JIRA+Remote+Issue+Links.

0 votes

You do realise that creating a custom field like that will add it to EVERY issue in your system? Ok, you are only populating one issue with any data, but the whole principle sounds wrong to me - if a user uses your webwork action 20 times, you'll end up with 20 new custom fields.

Even if that really is what you intend to do, it would help if you told us what error message you get (if any), what happens when you add the field to a screen so you can check the value, what the administration screens tell you about the new field you've created and so-on!

Let me give you a bit of background of my requirements,

I have created popup page which can be opened clicking a menu item I have added on the View Issue page. This popup page shows a list of video links, the user should be able to select any one on the link and save it as an attachment. Since, I dont have a file to attach, just a url to a video, I can not save it as an attachment.

So, what I thought is that, I would take the seleted link and save it in a custom filed with the current issue. I would later need to acces this custom filed, take the url, and try to embed the video the url points to, on the View Issue page, preferebly in the attachments section . Am I thinking it wrong? Is there any better way to implement my requirement?

When my code, I posted above, executes, I dont get any error. I am using the following code to retrieve the saved value:

CustomFieldType fieldType = customFieldManager.getCustomFieldType("com.atlassian.jira.plugin.system.customfieldtypes:textfield");
videoField = customFieldManager.createCustomField("videos", "Video link", fieldType , null, null, null);
log.info("CustomFieldValue" + issue.getCustomFieldValue(videoField));

Please help.

Here is what my administration panel looks like.a

So, let me get this clear (apologies for the extra questions - I often jump to incorrect conclusions and find that I should really ask more often to make sure I'm talking about the right thing).

1. You don't want to add a new custom field. You want to add (or update) data in a single, pre-existing custom field?

2. This custom field needs to do things that the current custom field types you have available do not support what you need (e.g. a plain text field would just show a url as plain text)?

As an aside, your admin screen shot shows part of the problem - you've got a set of duplicate fields all called videos, but they're not configured to be used anywhere. Duplicate names definitely will cause you problems, and I really don't think you want multiple "videos" fields, but the main thing here is that the fact that they're not configured for any context is why your code is never executed...

I am guessing here, but I'm not sure that you understand the difference between a "custom field", a "custom field type" and a "custom field value" - could you explain? I offer my humble apologies if you do know the difference, but from what I'm seeing, you're uncertain.

First of all let me tell you, I am new to Atlassian plugin development, so, I might have written/asked confusing question.

1. Yes.

2. The way I wanted to implement it, I just need to save a URL text in the custom filed.

Let me explain what I understand with those terms and please correct me if I am wrong:

Custom Field: Having an extra input field which Jira does not have.

Custom Field Type: What kind of data the filed will accept and save. The type of the input field, e.g. text, number, binary data.

CustomFieldValue: The actual data that is saved in a custom filed.

The following are my actual requirements:

(1) I want to create a popup/dialog page which would show a list of videos created in a external site. This popup will be tied with a View Issue page menu item.
(2) Users will be able to see the preview of the selected video link on the same popup.
(3) Users will be able to select any of the video link and save it as an attachment.
(4) The attached video link will be shown as an embedded video in the Attachments section of the View Issue page.

Probably I have gone about implementing it in the wrong way. Please suggest me the true path :-)

Ohh and everything has to be written as a Plugin.

Ok, that's nice and clear.

The problem you have got is that your code is creating new "custom fields" and it actually wants to be creating "custom field values". You don't want a new custom field each time someone uses the link, you just want a single custom field, which is then populated for issues as they need a video added.

The custom field type is what you need to do, and those are provided by plugins. My suggestion would be

  • Create a custom field type plugin, probably based on the "Text" custom field type
  • This field should be quite simple on the back-end (the java bits), and just store text - probably the url of the relevant video
  • The more clever bit would be the velocity templates:
    • The "view" template would need to implement the embedding and playback code
    • The "edit" template would need to enable the popup and select stuff

Some minor points about this

  • The administration of the field will be handled by the standard configuration inside Jira because it looks like any other custom field type as far as Jira is concerned. That way, you can add it to a project/issuetype just like the others, relying on the administrators to apply it properly in the right places (but only once - once the field is added, you won't need to touch it again)
  • I can see the full behaviour on the front-end being quite complex to code for,and I'll admit to being out of my depth on the html/java/javascript you'll need to drop into the velocity. If I were trying to do this, I'd start with a plain text field that a user can paste a url into and prove it gets saved and displayed correctly. Then, for "view", you can wrap the html for the player around the string coming back. Similarly for edit, you'd need to write the html for search/browse/preview so that it pokes the raw url back in
  • I think you'll need to provide a player library inside Jira to do this.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Oct 11, 2018 in Marketplace Apps

You + one app + a desert island...

Hi all! My name is Miles and I work on the Marketplace team. We’re looking for better ways to recommend and suggest apps that are truly crowd favorites, so of course we wanted to poll the Community. ...

2,012 views 5 6
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you