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

How to get a Jira Service Desk Request name in script runner, script fields ?

Maikes
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.
February 7, 2020

Hi Dear Colleagues from Atlassian, 

I want to get the request type name to compare in a [Switch Case] in Script Runner Fields. 

Do someaone knows how to help me ? 

 

Regards

1 answer

Suggest an answer

Log in or Sign up to answer
1 vote
Alejandro Suárez - TecnoFor
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
February 7, 2020

Hi @Maikes 

I guess that what you need is to get the name of the request type instead of his key (the value you got from getCustomFieldValue())

So here you go:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.user.ApplicationUser
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.onresolve.scriptrunner.runner.customisers.WithPlugin

@WithPlugin("com.atlassian.servicedesk")

String requestTypeName = getRequestTypeName(issue)
String scriptFieldResult

switch (requestTypeName) {
case "RequestTypeName1": scriptFieldResult = "Whatever I want to show on this request type"; break
case "RequestTypeName2": scriptFieldResult = "Whatever I want to show on this request type"; break
default: scriptFieldResult = "Whatever I want to show if i dont find any request type with the previous names"
}
return scriptFieldResult

static String getRequestTypeName(Issue issue) {
RequestTypeService requestTypeService = ComponentAccessor.getOSGiComponentInstanceOfType(RequestTypeService)
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
RequestTypeQuery requestTypeQuery = requestTypeService.newQueryBuilder()
.issue(issue.getId())
.build()
PagedResponse<RequestType> pagedResponse = requestTypeService.getRequestTypes(currentUser, requestTypeQuery)
return pagedResponse?.first()?.getName()
}
Alejandro Suárez - TecnoFor
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
February 7, 2020

Forgot to tell I'm assuming that you have JSD 4.0+ (Jira 8.0+), if you are using 7.X it wont work like that (SD API changes in 8.X).

If that's the case don't worry, I'll give you the correct code.

TAGS
AUG Leaders

Atlassian Community Events