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

Javascript: how to load function in "Create Issue" dialog

Declan Murphy April 15, 2015

I've written a small piece of javascript that automatically fills in the Summary field when creating a new issue. This works as intended when I open the "Create Issue" screen by right-clicking the "Create" button and opening it in a new tab or window. However, my script doesn't work when I open the screen by just clicking on it and opening the dialog.

How can I get my javascript to work in the "Create Issue" dialog?

Below is my javascript:

jQuery(document).ready(function($) {
    
    JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (e,context) {
        autoFillSummary();
    });

    autoFillSummary();

    // Automatically fill the summary field with a default value.
    function autoFillSummary(){
        var issueType = $('#issue-create-issue-type').text();
        var reporter = $('#reporter').val();
        var d = new Date();
        d = d.toDateString();
        var summaryVal = reporter + " - " + "[ " + d + " ]";
        if(issueType === "Job"){
            $('#summary').val(summaryVal);
            $('#summary').closest('div.field-group').hide();
            $('#reporter').closest('div.field-group').hide();
        }
    }  
});

Thank you for your help.

1 answer

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
Dmitrii Apanasevich
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 15, 2015

Hi!

First you need to be sure that Create issue dialog ready. 

You could use timeout

JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (e,context) {
       setTimeout(function() {autoFillSummary();}, 50);
    });

 

Second, there is no such element $('#issue-create-issue-type') in dialog but there is $('#issuetype').

 

 

Declan Murphy April 15, 2015

Thank you for the response. This doesn't do the job unfortunately. For now, I've worked around the issue by using [code]AJS.$("#create_link").removeClass("create-issue");[/code] to disable the pop-up window and force the Create Issue screen into a new page.

Dmitrii Apanasevich
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 15, 2015

Looks not good. You could find the bug in your code via debugging JavaScript https://developer.chrome.com/devtools/docs/javascript-debugging Maybe there is no $('#issuetype') too in your JIRA version.

Declan Murphy April 16, 2015

My debugging efforts seem to indicate that the script isn't firing when the "create issue" dialog open. Also, you're right, in the JIRA version I'm using, the element id is $('#issue-create-issue-type'). $('#issuetype') doesn't seem to work for me but as a precaution, I've added it to the condition as an OR expression. I'm new to JQuery and JIRA so the problem is likely due to my lack of knowledge but I have feeling the solution is rather straight-forward.

srinivasp
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 17, 2015

Hi Dmitrii, Your suggestion is perfectly working. Thanks for your work around !!

Declan Murphy April 20, 2015

I must apologize, Dmitrii, your solution was correct. My error was in what id I was using. For the "Create Issue" dialog, I had to use: AJS.$('#issuetype-field').val(); For the "Create Issue" page, I had to use: AJS.$('#issue-create-issue-type').text(); Now it's working perfectly. Thank you for your help. Much appreciated.

Dmitrii Apanasevich
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 20, 2015

You are welcome! :)

Lai Dai April 22, 2015

Hi All, I have the opposite case. My javascript only be called in "create issue" dialog but not "create issue" in new tab or new window. Here is the simplified version of my javascript. jQuery(document).ready(function($) { JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (e,context) { alert("create"); }); alert("create in new window"); }); When creating an issue in the dialog, "create" message is shown. When creating an issue in new tab or window, no alert message is shown. When searching issues, "create in new window" message is shown. It seems the javascript is not invoked when creating issue in new tab or window. Please help.

srinivasp
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 22, 2015

Can you try this? jQuery(document).ready(function() { if (String(document.location.href).indexOf("CreateIssue.jspa") >= 0) { alert('') } });

Lai Dai April 22, 2015

Hi Srinivas, Thanks for your reply. I tried your suggestion but the alert message is still not shown in new tab or new window when creating issue. I think the javascript is not loaded in this case. By the way, I have my javascript as a web-resource of a plugin which I added to my JIRA instance. I am not sure if that changes anything.

Lai Dai April 22, 2015

Here is the web resource I defined in my plugin. <web-resource key="javascriptplugin-resources" name="javascriptplugin Web Resources"> <dependency>com.atlassian.auiplugin:ajs</dependency> <context>atl.general</context> <resource type="download" name="javascriptplugin.js" location="js/javascriptplugin.js"></resource> </web-resource>

srinivasp
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 22, 2015

Can you try this and see if this is working? jQuery(document).ready(function() { alert((document.location.href).indexOf("CreateIssue.jspa") ) }); My JS is working is same way but I am not injecting as dependency. In our case it was simple hack so we are calling from JS file from Announcement banner.

Lai Dai April 23, 2015

Instead of using JQuery document ready function, I used AJS function below and it solved the problem. AJS.$(document).ready(function($)

TAGS
AUG Leaders

Atlassian Community Events