Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Atlassian Jira. Admin evolution. Part 5

Part 4

This solution should be avoided at all cost!

A bit more JSP

One thing left. I set the Approver as manager1 all the time, but I need to get the manager for the reporter from my user_managers table. How to do it?

Again we can create a jsp page. Let's call this page getmanagerforuser.jsp with the following code:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%
try {
   String reporter=request.getParameter("reporter");
   response.getWriter().write("{\"reporter\":\""+ reporter+ "\",\"manager\":\"manager1\"}");

}   catch(Exception e)  {
}
%>

This code gets the url parameter which is called reporter and produces a json where the value of the reporter field is the value of the url parameter and the value of the manager field is manager1. Let's call this jsp page:

Screenshot 2020-06-09 at 19.41.07.png

As you can see I passed the myreporter value as the reporter parameter and the json file contains the value of the passed parameter.

Good! Now we need to call our jsp page from our JavaScript code. We will pass the value of the reporter field and we will get the manager. We can do it with the following JavaScript line:

            $.get( "/jira/getmanagerforuser.jsp?reporter="+ $("optgroup#reporter-group-suggested > option:selected").val(), function( data ) {
                 $("#customfield_10300").val(data.manager);
            });

In this line we call the getmanagerforuser.jsp, we pass the value of the reporter field and get the manager. Let's open the create issue screen:

Screenshot 2020-06-09 at 19.59.17.png

The field is empty. If we have a look at the Developer Tools we will see:

Screenshot 2020-06-09 at 20.05.35.png

The jsp page was called correctly. The reporter parameter has the value admin and it is correct. Let's have a look at the response:

Screenshot 2020-06-09 at 20.07.17.png

As you can see our json is just a part of an html content and we need to cut the json from the content. Lets cut it. Here is the code:

 $.get( "/jira/getmanagerforuser.jsp?reporter="+ $("optgroup#reporter-group-suggested > option:selected").val(), function( data ) {
                 var startIndex = data.indexOf("<section id=\"content\" role=\"main\">") +34;
                 var endIndex = data.indexOf("</section>", startIndex);
                 var str = data.substring(startIndex, endIndex);
                 $("#customfield_10300").val(JSON.parse(str.trim()).manager);
            });

And now if we create an issue we will see that the Approver field has the manager1 value:

Screenshot 2020-06-09 at 20.20.33.png

Here is the final JavaScript code:

<script>
      setInterval(function(){ 
         if ($("optgroup#reporter-group-suggested > option:selected").val()) {
            $.get( "/jira/getmanagerforuser.jsp?reporter="+ $("optgroup#reporter-group-suggested > option:selected").val(), function( data ) {
                 var startIndex = data.indexOf("<section id=\"content\" role=\"main\">") +34;
                 var endIndex = data.indexOf("</section>", startIndex);
                 var str = data.substring(startIndex, endIndex);
                 $("#customfield_10300").val(JSON.parse(str.trim()).manager);
            });

            
            $("#customfield_10300").focus(function() {
                 this.blur();
            });
         }
      }, 3000);
      setInterval(function(){ 
         if ($("#customfield_10300-val").text()) {
           if (JIRA.Users.LoggedInUser.userName() != $.trim($("#customfield_10300-val").text())) {
            $('#action_id_11').addClass('hidden');
           }
         }
      }, 3000);
</script>

And now we need to change our getmanagerforuser.jsp so that the manager would be taken from our database table. I will not provide this code, but you know how it can be done.

At last the jsp part and js part are over. Only one requirement left.

Part 6

1 comment

Mohammed Amine Community Leader Sep 26, 2020

good to know.

Comment

Log in or Sign up to comment
TAGS
Community showcase
Published in Apps & Integrations

Codegeist '21 Customer Choice Awards - Vote Now!

Last quarter, 290 developers participated in Atlassian’s annual hackathon, Codegeist, for a chance to win thousands of dollars with a new Marketplace app. We announced winners selected by Atlassian...

915 views 4 16
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you