Javascript in description of the customfield is not running after upgrade to Jira 6.0

elena slobodnik March 6, 2014

We have the Jira plugin that consists of two multi-select custom controls: Clients and ClientContacts. Both controls are data-bound/populated at form load of CreateIssue and then once Contacts selection changes, data in ClientContacts is filtered based on the new value(s) in Contact. To achieve that all we need is for javascript (below) to be imbedded into jiraschema.fieldlayoutitem.Description filedof both custom controls . The functionality worked in Jira 5.0 , but is no longer supported in Jira 6.0 due to the security reasons.

https://jira.atlassian.com/browse/JRA-28776

Question: is there another/new field or configuration setting that will allow Javascript to execute/work same way as Description field in Jira 5.0? If not, what is the way to deliver the same functionality without extensive redesign of the plugin?

<script type="text/javascript"> AJS.$(document).ready(main()); function main() { var debug = false if(debug) { webBase = "http://localhost:2990/jira"; //webBase = "http://localhost:8080"; // Custom Fields client_customfield = "customfield_10000"; contact_customfield = "customfield_10001"; phone_customfield = "customfield_10002"; version_customfield = "customfield_10003"; } else { webBase = "https://jira.mapcom.com"; //webBase = "http://localhost:8080"; static_client_customfield = "customfield_10000"; // Custom Fields client_customfield = "customfield_10251"; contact_customfield = "customfield_10252"; phone_customfield = "customfield_10253"; version_customfield = "customfield_10237"; // Hide static Client custom field var oldContact = document.getElementById(static_client_customfield); if (typeof(element) != 'undefined') { oldContact.style.visibility = 'hidden'; findLableForControl(oldContact).innerHTML = ""; } } addContactListener("#" + contact_customfield,"change"); } function refresh() { var debug = false if(debug) { webBase = "http://localhost:2990/jira"; //webBase = "http://localhost:8080"; // Custom Fields client_customfield = "customfield_10000"; contact_customfield = "customfield_10001"; phone_customfield = "customfield_10002"; version_customfield = "customfield_10003"; } else { webBase = "https://jira.mapcom.com"; //webBase = "http://localhost:8080"; static_client_customfield = "customfield_10000"; // Custom Fields client_customfield = "customfield_10251"; contact_customfield = "customfield_10252"; phone_customfield = "customfield_10253"; version_customfield = "customfield_10237"; // Hide static Client custom field var oldContact = document.getElementById(static_client_customfield); if (typeof(element) != 'undefined') { oldContact.style.visibility = 'hidden'; findLableForControl(oldContact).innerHTML = ""; } } addContactListener("#" + contact_customfield,"change"); } refresh(); JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function(e, context) { refresh();}); function findLableForControl(el) { var idVal = el.id; labels = document.getElementsByTagName('label'); for( var i = 0; i < labels.length; i++ ) { if (labels[i].htmlFor == idVal) return labels[i]; } } function addContactListener(field, event) { AJS.$(field).bind(event, function(e) { getPhoneBasedOnClientAndContact(field,event); }); } function getHTTPObject() { if (typeof XMLHttpRequest != 'undefined') { return new XMLHttpRequest(); } try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { } } return false; } function populatePhoneNumber(http) { var phone = document.getElementById(phone_customfield); if (http.readyState == 4){ if(http.status == 200){ var number = http.responseXML.getElementsByTagName('value'); phone.value = number[0].firstChild.nodeValue; } } } function trim(s) { s = s.replace(/(^\s*)|(\s*$)/gi,""); s = s.replace(/[ ]{2,}/gi," "); s = s.replace(/\n /,"\n"); return s; } function getPhoneBasedOnClientAndContact(field,event) { var myContacts = document.getElementById(contact_customfield); var myClients = document.getElementById(client_customfield); var myPhone = document.getElementById(phone_customfield); var clientval = myClients.value var client_text = trim(myClients[myClients.selectedIndex].textContent) var contact_text = trim(myContacts[myContacts.selectedIndex].textContent) if(myContacts.value == "-1" || contact_text == "Not Available"){ myPhone.value = ""; return; } // If client was not first selected if(clientval == "-1"){ clientval = ""; } var path = webBase + "/rest/crm-rest/1.0/phone?key=" + escape(contact_text + " | " + client_text); var con = getHTTPObject(); con.open("GET", path, true); con.onreadystatechange = function() { populatePhoneNumber(con); } con.send(null); } </script>


3 answers

0 votes
Marco Mancuso
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 11, 2014

Hello Elena,

One of our biggest customer faced the same issue with 400+ of js into fieldconfig descriptions.

The biggest issue is the interaction with the quick create screen because the JS seems to be loaded only during the 1st load of the popup and if another project or issuetype was selected the JS was not loaded anymore.

We tried the Message Custom field approach but while it was working perfectly with workflow screens we had the same issue with the quick create screen.

We decided to use the Behaviours plugin, and looking at what your code does you should be able to easily convert your javascript into a behavior https://marketplace.atlassian.com/plugins/com.onresolve.jira.plugin.Behaviours

You will be able to use the behaviours server side script almost for everything, the only tricky part is the ajax call. You have two options:

  1. expose the service as an OSGI component in your jira plugin and then use the normal behaviours server side script
  2. Use a workaround that relies on javascript :)
//Server side evaluation
FormField descField = getFieldById("description")
if (descField.getFormValue() == "Ciao") {
     //Javascript injection
     descField.setHelpText("&lt;script&gt;alert('"+descField.getValue()+"')&lt;/script&gt;")
}

Marco

0 votes
Fabio Racobaldo _Herzum_
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 11, 2014

Hi Elena,

you could add your javascript into the description of the related custom field of the Field Configuration Scheme used into your project(s).

it works also in JIRA 6.X. https://jira.atlassian.com/browse/JRA-28776has been resolved with resolution WON'T FIX.

Try to clean your browser cache first.

Hope this helps.

Fabio

0 votes
John Bishop
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 11, 2014

We use the Message Custom Field from the JIRA Toolkit Plugin (https://marketplace.atlassian.com/plugins/com.atlassian.jira.toolkit).

The javascript goes in the custom field's default value.

Chris K. December 13, 2014

I came across this post and I'm trying use some javascript to put a custom field value in a Message Custom Field (no edit). As you suggested, I put the script in the default value field but it doesn't seem to be working. If I put the same code regular single-line text custom field, it works fine. Here's my script: <script type="text/javascript"> summary = document.getElementById('summary'); issue_key = AJS.Meta.get("issue-key"); target = document.getElementById('customfield_10002'); target.style.display = ''; target.value=issue_key + " " + summary.value; </script> As you can see, I'm just trying to put the Issue Key and the Summary in the field. Any thoughts?

Suggest an answer

Log in or Sign up to answer