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

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

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.

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?

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

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

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,965 views 12 18
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot