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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

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

Creating customer request using JAVA API's doesn't work

I'm using this code to create customer request using JAVA API's

 

serviceDeskCustomerRequestService.
createCustomerRequest
(applicationUser, serviceDeskCustomerRequestService.newCreateBuilder().raiseOnBehalfOf("username")        .serviceDesk(serviceDesk).requestType(requestType).fieldValue(FieldId.withId("summary"),     FieldInputValue.withValue("summary value")).fieldValue(FieldId.withId("description"),             FieldInputValue.withValue("description value")).customerRequestChannelSource("Customer Channel Value").build());

 

If I used it as it is, it works, but when using it inside a schedular (thread) it stopped working for the below issue

 

com.atlassian.servicedesk.api.BadRequestException: The issue type selected is invalid.  com.atlassian.servicedesk.api.BadRequestException: The issue type selected is invalid.    at com.atlassian.servicedesk.internal.api.util.EitherExceptionUtils.httpStatusCodeToException(EitherExceptionUtils.java:95)    at com.atlassian.servicedesk.internal.api.util.EitherExceptionUtils.lambda$anErrorEitherToException$0(EitherExceptionUtils.java:36)    at io.atlassian.fugue.Either$Left.fold(Either.java:586)    at com.atlassian.servicedesk.internal.api.util.EitherExceptionUtils.anErrorEitherToException(EitherExceptionUtils.java:32)    at com.atlassian.servicedesk.internal.feature.customer.request.ServiceDeskCustomerRequestServiceImpl.createCustomerRequest(ServiceDeskCustomerRequestServiceImpl.java:45)

 

Anyone is facing this issue? 

1 answer

0 votes
Sam Nazha Atlassian Team Dec 21, 2020

Hi Mohamad,

 

Thanks so much for your question.

 

Could you kindly provide some details around your environment and job?

  1. What version of Jira are you running?
  2. Are you able to post the sample source code that causes the issue?

 

Thank you,

Sam.

Jira Service Management

Thanks Sam for your reply,

 

Below is the code that we're trying out

 

package com.infosysta.twitter4jira.servlet;

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.servicedesk.api.ServiceDesk;
import com.atlassian.servicedesk.api.ServiceDeskService;
import com.atlassian.servicedesk.api.field.FieldId;
import com.atlassian.servicedesk.api.field.FieldInputValue;
import com.atlassian.servicedesk.api.request.CustomerRequestChannelSource;
import com.atlassian.servicedesk.api.request.CustomerRequestCreateParameters;
import com.atlassian.servicedesk.api.request.ServiceDeskCustomerRequestService;
import com.atlassian.servicedesk.api.requesttype.RequestType;
import com.atlassian.servicedesk.api.requesttype.RequestTypeQuery;
import com.atlassian.servicedesk.api.requesttype.RequestTypeService;
import com.atlassian.servicedesk.api.util.paging.PagedResponse;
import com.infosysta.twitter4jira.utils.logger.ISLogger;
import com.infosysta.twitter4jira.utils.serviceDesk.ServiceDeskCompatibility;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class WebServlet_Test extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String username = req.getParameter("username");
            String projectKey = req.getParameter("projectKey");

            Thread thread = new Thread(new Runnable() {

                public RequestType getRequestTypeById(ApplicationUser loggedInUser, ServiceDesk serviceDesk, int requestTypeId) {
                    RequestType selectedRequestType = null;
                    try {
                        RequestTypeService requestTypeService = ComponentAccessor.getOSGiComponentInstanceOfType(RequestTypeService.class);
                        RequestTypeQuery.Builder requestTypeQueryBuilder = requestTypeService.newQueryBuilder();
                        RequestTypeQuery requestTypeQuery = requestTypeQueryBuilder.requestType(requestTypeId).serviceDesk(serviceDesk.getId()).build();
                        PagedResponse<RequestType> requestTypes = ServiceDeskCompatibility.getPagedResponse(requestTypeService.getRequestTypes(loggedInUser, requestTypeQuery));
                        if (requestTypes != null) {
                            List<RequestType> requestTypesList = requestTypes.getResults();
                            for (RequestType requestType : requestTypesList) {
                                if (requestType.getId() == requestTypeId) {
                                    selectedRequestType = requestType;
                                    break;
                                }
                            }
                        }
                    } catch (Exception e) {
                        ISLogger.getLogger().writeExceptionLog(e);
                    }
                    return selectedRequestType;
                }

                @Override
                public void run() {
                    try {
                        ApplicationUser applicationUser = ComponentAccessor.getUserManager().getUserByName(username);
                        Project project = ComponentAccessor.getProjectManager().getProjectByCurrentKey(projectKey);

                        ServiceDeskService serviceDeskService = ComponentAccessor.getOSGiComponentInstanceOfType(ServiceDeskService.class);
                        ServiceDesk serviceDesk = serviceDeskService.getServiceDeskForProject(applicationUser, project);
                        long requestTypeId = 1;//ISUtilities.toLong(req.getParameter("requestTypeId"));
                        RequestType requestType = getRequestTypeById(applicationUser, serviceDesk, (int) requestTypeId);
                        ServiceDeskCustomerRequestService serviceDeskCustomerRequestService = ComponentAccessor.getOSGiComponentInstanceOfType(ServiceDeskCustomerRequestService.class);

                        CustomerRequestCreateParameters customerRequestCreateParameters = serviceDeskCustomerRequestService.newCreateBuilder()
                                .raiseOnBehalfOf(applicationUser.getUsername())
                                .serviceDesk(serviceDesk)
                                .requestType(requestType)
                                .fieldValue(
                                        FieldId.withId("summary"),
                                        FieldInputValue.withValue("From Servlet")
                                )
                                .fieldValue(
                                        FieldId.withId("description"),
                                        FieldInputValue.withValue("From Servlet")
                                )
                                .customerRequestChannelSource(CustomerRequestChannelSource.withValue("Servlet"))
                                .build();

                        Object customerRequest = serviceDeskCustomerRequestService.createCustomerRequest(applicationUser, customerRequestCreateParameters);
                    } catch (Exception e) {
                        ISLogger.getLogger().writeExceptionLog(e);
                    }
                }
            });
            thread.start();
        } catch (Exception e) {
            ISLogger.getLogger().writeExceptionLog(e);
        }
    }
}

 

 The issue is due to

ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

 it returns null inside the thread

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira Service Management

Why upgrade to Jira Service Management Premium?

We often have questions from folks using Jira Service Management about the benefits to using Premium. Check out this video to learn how you can unlock even more value in our Premium plan.  &nb...

165 views 0 4
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