Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

putting javascript in the description field of a customfield via plugin does not show up in jira

Umair Haroon
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 23, 2014

Hi , I am putting javascript code in a String variable via a plugin class that would install the custom field. When I put text in the variable it shows up fine in the description of the field in JIRA , but when its javascript code, it does not show up at all.

I took care to properly format the js code (adding escape chars where neccessary) but still it doesnt show up.

Is it possible to add javascript code as description for a custom field we are creating and have you done it before (via a plugin) ?

4 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Answer accepted
MattS
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 24, 2014

I'm also bitten regularly during upgrades by bits of JavaScript in field descriptions. You start with it in one place, then it gets copied and modified to other fields, none of it version controlled in any way. You change the JIRA release and suddenly you have 25 different places in JIRA you have to fix scripts. The Behaviours plugin is a better solution in general.

1 vote
Boris Georgiev _Appfire_
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 23, 2014

I'll repeat my comment from https://answers.atlassian.com/questions/286401/how-can-i-put-javascript-code-to-spice-up-a-multi-select-custom-field-i-am-creating-in-a-pluginhere

Jira allows setting js code in the description. I think you do not need to escape it. The script will not show up in the description but it's there. You can do a simple test by putting <script>console.log('test message')</script> and check if it will show up in the browser console.

https://confluence.atlassian.com/display/JIRA/Fields+Allowing+Custom+HTML+or+JavaScript

Umair Haroon
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 23, 2014

Boris the reason I need escape sequence is as follows:

String jsCode = "<script type=\"text/javascript\">....." so on

Also the link you gave seems to be about inserting javascript code to the field via JIRA (running instance). I am talking about setting the description with the js from code (via a plugin).

Boris Georgiev _Appfire_
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 23, 2014

It is the same actually. Can you provide the code you're currently using ?

Umair Haroon
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 23, 2014

Sure. See below:

0 votes
Umair Haroon
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.
May 11, 2014

@jamieif you could provide the following comment:

" I would create a new CF type with a different template, and then have a js resource that looks for these cf types on page load and manipulates them. Bit more hassle, but could be worth it in the end."

as an answer I would love to mark it as correct answer as its a good point of view of yours.

Thanks

0 votes
Umair Haroon
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 23, 2014

               //create a list of issue types for which the screen needs to be available
		List<GenericValue> issueTypes = new ArrayList<GenericValue>();
		issueTypes.add(null);
		
		//create a list of project contexts for which the custom field needs to be available
		List<JiraContextNode> contexts = new ArrayList<JiraContextNode>();
		contexts.add(GlobalIssueContext.getInstance());
		
		//get params to create custom field
		String customFieldTypeKey = "com.atlassian.jira.plugin.system.customfieldtypes:multiselect";
		String customFieldSearcherKey = "com.atlassian.jira.plugin.system.customfieldtypes:multiselectsearcher";

                CustomFieldSearcher customFieldSearcher = customFieldManager.getCustomFieldSearcher(customFieldSearcherKey);
		
		//create custom field
		CustomField cField = customFieldManager.createCustomField(customfieldName, "", customFieldManager.getCustomFieldType(customFieldTypeKey) , customFieldSearcher, contexts, issueTypes);

                StringBuilder jsCode = new StringBuilder("<script type=\"text//javascript\">");
                //remove none option from multiselect field
jsCode.append("AJS.$(\"#" + cField.getId() + " option[value='-1']\").remove();"); //add javascript code to make the multiselect field look fancy like the components multiselect field jsCode.append("(function($)"); jsCode.append("{"); jsCode.append("new AJS.MultiSelect("); jsCode.append("{"); jsCode.append("element: $(\"#" + cField.getId() + "\"), itemAttrDisplayed: \"label\", errorMessage: AJS.params.multiselectComponentsError"); jsCode.append("});"); jsCode.append("}"); jsCode.append(")(AJS.$);"); jsCode.append("<//script>"); cField.setDescription(jsCode.toString()); customFieldManager.updateCustomField(cField);
//add multiselect custom field to default screen FieldScreen defaultScreen = fieldScreenManager.getFieldScreen(FieldScreen.DEFAULT_SCREEN_ID); if(!defaultScreen.containsField(cField.getId())) { FieldScreenTab firstTab = defaultScreen.getTab(0); firstTab.addFieldScreenLayoutItem(cField.getId()); }
Boris Georgiev _Appfire_
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 23, 2014

Why are you putting '//' in the close html tags ? "<//script>" ?

Umair Haroon
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 23, 2014

Boris, you are totally correct.

Costly oversight.

Going to accept your answer in the context of this conservation.

JamieA
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 23, 2014

Ouch... good spot Boris.

For me embedding code in strings is a bad smell... I pity the person that tries to maintain this. FWIW, I would create a new CF type with a different template, and then have a js resource that looks for these cf types on page load and manipulates them. Bit more hassle, but could be worth it in the end.

Umair Haroon
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 23, 2014

Jamie actually you are correct from a maintenance point of view in the long term it is not ideal. However I just wanted to have a multiselect field which looks like the components field. And the javascript is there just to make an ordinary multiselect field look like the components field.

Boris Georgiev _Appfire_
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 23, 2014

Jamie, I agree with you, but the original question was around injecting JS code into the description, so we drilled down on that topic without thinking about the potential problems :).

Umiar, I advice you to consider Jamie's suggestion, to avoid future headaches with that code.

Umair Haroon
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 23, 2014

Noted. However, as you mentioned I was merely curious if its possible to inject js that way via a plugin and as to why it wasnt working (if it was allowed).

JamieA
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 24, 2014

Boris, sure, it wasn't meant as a criticism. I was just offering my experience, unasked ;-)

Like many people I have been bitten by having javascript in velocity templates.

Boris Georgiev _Appfire_
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 24, 2014

Jamie, I'm always happy to hear your opinion as we all really can learn a lot from you.

Umair Haroon
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 27, 2014

Boris and @jamie, what if i were to put the js code a .js file embedded with the plugin and have the plugin class just read the .js file from there.

In this way the code is easily readable and can be easily modified.

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events