Get issueID in JavaScript in Project Screen (viewing issue)

Christian_Schönert June 19, 2018

Hello to all,

 

I am having a problem where I have no idea where to find a solution or what to search for, so I'm asking this Question. A little context ahead:

I'm writing a plugin that shows some additional information on the ViewIssueScreen. Placing it right on the standard viewing screen for issues (like eg. structure plugin etc.). I use some velocity and JavaScript to postprocess and display some informations. To do that I'm using my own REST Resource which requires the issueID to work with. I could also use the issueKey, but at this point this doesn't change anything or help.

So I found this https://community.atlassian.com/t5/Answers-Developer-Questions/Read-current-issue-id-with-javascript/qaq-p/481560 to get to the issueID (or key) with

 var issueID = JIRA.Issue.getIssueId();
 var issueKey = AJS.$("meta[name='ajs-issue-key']").attr("content");

This all works fine up until this:

 

When I go to the project of the issue, where all issues that belong to it are listed on the left, and the issue clicked is displayed in a screen right side next to it, it can't retrieve the id or key anymore. It just gives back null and undefined.

How can I get the issueID or issueKey within the ViewIssueScreen where it doesn't matter if its a subpage or not?

1 answer

1 accepted

2 votes
Answer accepted
Mauricio Karas
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 20, 2018

Hello, Christian. Thanks for reaching out to the Atlassian Community!

I've tried getting the issue key in the issue navigator with the AJS.$("#key-val").text() command but the same behavior happened, it returned null. After some search I was able to find out that it might be because the callback is pointing to the first HTML that rendered which doesn't have the class with the "key-val" id, the problem being the inline editing.

This other question from the Developer Community is describing something similar: Parts of javascript not working in issue navigator

One of the answers suggested is listening to a Jira JS event and then executing the code inside the listener function. I've created the following code and it seems to be working for the purpose of retrieving the issue key.

JIRA.bind(JIRA.Events.ISSUE_REFRESHED, function() {
var key = AJS.$("#key-val").text();
console.log("Issue Key: " + key);
});

There's further information on extending the inline editing in Jira is this page: Extending inline edit for JIRA plugins

Kind regards,
Maurício Karas

Christian_Schönert June 21, 2018

This is likely the most valuable answer I have ever received, thank you very much, also for the quick response. So now I'm calling my setup function from this event bind and pass the issue-key and it works smoothly.

I didn't know about all this inline stuff, this will help a lot in the future too.

So maybe you have time for a little more follow-up questions out of curiosity:

Why is it that always people recommend using the issue-key and never issue-id? I mean if they are both unique, why even bother to have two?

How would I get the ID then? AJS.$('#id-val")?

 

Best regards,

Chris

Mauricio Karas
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 21, 2018

Glad to hear it worked on your end and that I was able to help.

I don't know exactly why is that the issue key is more used, you're right, they're both unique. I think it's because the issue key is more meaningful than the id, the issue key shows up in the Jira GUI so you can easily find what issue you are referring to.

Yeah, to get the id, the "id-val" doesn't seem to work. If you inspect the issue key next to the project's name you can see rel="<id>", which is the id you want. Another way that I've found, which I can see people using, is the following:

JIRA.Issue.getIssueId()
JIRA.Issue.getIssueKey()

This worked for me and I think is more readable than getting fields and HTML elements.

Kind regards,
Maurício Karas

Christian_Schönert June 26, 2018
JIRA.Issue.getIssueId()
JIRA.Issue.getIssueKey()

 This is of course the solution I tried at first (as stated), but this is exactly, where the inline doesn't work anymore. Imho it should still work then.

Nikita_Ishkov August 28, 2019

It's broken for next-gen projects.

Shilpa Sanil August 3, 2021

Similar way is it possible to get the value for a customfield?

Christian L November 7, 2023

It's broken for next-gen projects.

Is there a solution for next-gen projects in 2023?

Edit: at least the issue is closed, but not solved
https://jira.atlassian.com/browse/JRACLOUD-72940 

Suggest an answer

Log in or Sign up to answer