| New Plugin Under Development The Database Values JIRA Plugin will allow users to populate custom field with values extracted from a database. |
JIRA 3.2 and onwards support the ability to enter arbitrary HTML (as opposed to raw text) as the 'description' of a custom field. Typically this is just used to format the description (eg. add some hyperlinks), but the description can be used to do more interesting things, like setting the custom field value.
Pre-determined text field values
For instance, say we have a 'Class Name' custom field, which must be populated with a hard-to-remember string (a real example here; recording a class name):

In our description, we can add a list of valid options, which when clicked will populate the text field:


To do this:
- Go to Administration -> Issue Fields -> Custom Fields, and locate the custom field you wish to customize
- Click "Edit" on the custom field you are interested in
- Note the custom field ID in the URL. For example, the URL might include '/secure/admin/EditCustomField!default.jspa?id=10030', in which case the custom field ID is 10030.
- Paste the following into the custom field description:
<script type="text/javascript"> function showOptions() { var listenersDiv = document.getElementById("presetValues"); var listenersArrow = document.getElementById("presetValuesArrow"); if (listenersDiv.style.display == 'none') { listenersDiv.style.display = ''; listenersArrow.src='../images/icons/navigate_down.gif'; } else { listenersDiv.style.display='none'; listenersArrow.src='../images/icons/navigate_right.gif'; } } function setPresetValue(clazz) { var classField = document.getElementById("customfield_10030"); classField.value = clazz; classField.focus(); return false; } </script> <img id="presetValuesArrow" src="../images/icons/navigate_right.gif" width=8 height=8 border=0> <a href="#" onclick="showOptions(); return false;">Preset Options</a> <div id="presetValues" style="font-size: 90%; display: none"> <ul> <li> <a href="#" onclick="return setPresetValue('com.atlassian.jira.service.services.export.ExportService');">Backup service</a> </li> <li> <a href="#" onclick="return setPresetValue('com.atlassian.jira.service.services.pop.PopService');">Create issues from POP</a> </li> <li> <a href="#" onclick="return setPresetValue('com.atlassian.jira.service.services.imap.ImapService');">Create issues from IMAP</a> </li> <li> <a href="#" onclick="return setPresetValue('com.atlassian.jira.service.services.file.FileService');">Create issues issues from local files</a> </li> <li> <a href="#" onclick="return setPresetValue('com.atlassian.jira.service.services.DebugService');">Debugging service</a> </li> </ul> </div>
- In the pasted text, edit the line:
var classField = document.getElementById("customfield_10030");
and change the ID to match that of your custom field.
- Save the field, and try out the custom field on an issue.
Hidden extra fields
As another example, say we wish to hide some extra fields until a checkbox is ticked:

When ticked, an extra custom field displays:

This can be done with the following Javascript on the description of the bottom-most element (here, the 'Build Number' field):
<script type="text/javascript">
checkbox = document.getElementById('customfield_10013_1');
target = document.getElementById('customfield_10000FieldArea');
// Hide the target field by default
target.style.display = 'none';
checkbox.onclick=function() {
if (checkbox.checked) {
target.style.display = '';
} else {
target.style.display='none';
}
};
</script>
Other possibilities
This is a fairly tame demonstration, but there are many more interesting possibilities.
- Display a set of images to pick from, as a graphical alternative to a select-list.
- Populate multiple fields at once.
- Display an image, and store the clicked coordinates. For instance, display a map, and store latitude and longitude in two custom fields.
- Using XMLHttpRequest and Ajax techniques, query a database to populate a list or trigger a field update.
- Load an applet (perhaps using widgets from JDNC to present extra info relating to a field.

Comments (13)
Mar 02, 2006
Sikhar J Saikia says:
Is there any way to use this for validation? for example to check that some fiel...Is there any way to use this for validation? for example to check that some field is empty or not? But for that I need to capture the submit event or onblur; which I tried but couldn't do
Any suggestions??
Mar 02, 2006
Dylan Etkin says:
You should be able to do what your after with something like this: <script typ...You should be able to do what your after with something like this:
Where myFunction is where you do your validation.
Hope this helps,
Dylan
Apr 04, 2006
Sikhar J Saikia says:
If we click on Field Configuration we can edit the description of Standard field...If we click on Field Configuration we can edit the description of Standard fields also; is it possible to impose above mentioned Java Scripts in Standard fields as well? I tried an alert and it worked; but the problem I am facing so far is to retrieve any particular field using something similar to
var classField = document.getElementById("customfield_10002");
I was using fieldlayoutitem table to get the ids for the system field.
Apr 04, 2006
Nick Menere says:
Sikhar, You should not be using the ids, but rather the ids from the THML. Eg. t...Sikhar,
You should not be using the ids, but rather the ids from the THML. Eg. the resolution system field will have the id: resolution
Cheers,
Nick
May 12, 2006
Neil Arrowsmith says:
Does anyone know how I can get the issue number using javascript, and put it int...Does anyone know how I can get the issue number using javascript, and put it into a custom text field?
May 15, 2006
Justin Koke says:
Hi Neil, In what screen are you trying to do this? Edit? If so you will need to...Hi Neil,
In what screen are you trying to do this? Edit? If so you will need to use the DOM to get to it.
Unforunatley on the edit page the id of the DOM element is actually:
issue_key_TST-1, so you may like to modify this JSP to not have the issue id present in the element id.
The element in question is the following:
<a id="issue_key_TST-3" href="/browse/TST-3">TST-3</a>
Regards,
Justin
Nov 05, 2006
gumshoes says:
Such js is very useful to me, is there any way to get more information about the...Such js is very useful to me, is there any way to get more information about the current context? Things that interest me at present are:
Ideally I would like to be able to access all the available data about the current context. Is this possible and if so is this documented somewhere?
Thanks.
Nov 07, 2006
Jeff Turner says:
If you "view source" you see everything the JavaScript has access to.If you "view source" you see everything the JavaScript has access to.
Feb 28
Paul Csapo says:
Dear Jeff, and guys. Reading above, it says that we can "Populate multiple field...Dear Jeff, and guys.
Reading above, it says that we can "Populate multiple fields at once". This sounds good but leads me to some questions:
Let's imagine that I have a large form, with a field called "field x", and I then specify the content for this particular field (either by typing it in or selecting from options etc).
Q1)
Could I make several other fields within this form to suddenly be populated with something, depending on what I specified in the "x" field?
Q2)
Would it matter what field type "field x" was? (or what type the other fields i want to subsequently populate were?
Q3)
Could "field x" be on the 1st tab of the form, but the other fileds on different tabs?
Q4) (on a similar theme)
If I had a custom field called RELATED ISSUE NUMBER, (where something like "JIRA-1131" could be entered).
When they typed in that issue number (on "this" form), would there be a way to "retrieve" the particular field value based on what the value was for "that" issue number, and to then populate it in the appropriate field elsewhere on "this" form?
(I know an in-built Jira feature exists called "Issue Linking", but I'm thinking more in terms of using subtasks to help split out very large forms, but to still be able to pre-populate certain fields (for completeness), depending on what the Parent case contained).
If anyone can shed any light on this it will be really appreciated.
kind regards,
Paul
Feb 28
Jeff Turner says:
You have the HTML DOM object for the whole page, including tabs, so yes, you can...You have the HTML DOM object for the whole page, including tabs, so yes, you can populate whatever fields you wish on any tab. Firebug is very useful for exploring the DOM to see what Javascript could potentially do.
For information not already on the page, I imagine it's possible to do an XMLHttpRequest call to retrieve the XML view of another issue. I haven't tried it myself though.
Feb 29
Paul Csapo says:
Many thanks Jeff, Now that the theory sounds "sound", I can go on a developers j...Many thanks Jeff,
Now that the theory sounds "sound", I can go on a developers journey
thanks,
Paul
Apr 04
Abhishek Tripathi says:
Hi all, I want to add a date field depends on 'Select' field's value. Select Fie...Hi all,
I want to add a date field depends on 'Select' field's value.
Select Field Name:DownLoad?
Values
YES
NO
When user select YES, A date picker option should appear, otherwise none.
I tried above code but couldn't success.
Any idea, please?
Regards,
Abhishek
Apr 12
Wish J says:
Hi, I have a custom field of "user picker" type. I want the default value ...Hi,
I have a custom field of "user picker" type. I want the default value for this field should be the current login username ...
And if the value is already entered for that field then the value should be the existing value. (while editing the issue).
eg.. I have this field on the Create and edit screen of the issues.. Now My requirement, when I go to create the issue screen the default value for that field should be the current login username... and when I am editing the issue, the field value must be the existing value.
Any help on how can I apply this,..??
Thanks,
- Vishal