Set Customer Request Type to Issue Type using Scriptrunner Listener

Jorge Jerez January 8, 2021

Hi

 

I would like to know if someone knows how to set the Customer Request Type using an scriptrunner listener. 

In my case, I want to set a Customer Request Type with the same name as the Issue Type, this should work on different projects which shares issue types and request types. 

I've tried with this: 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.event.type.EventDispatchOption;

String CORRECTIVO = "Correctivo"

ApplicationUser user = ComponentAccessor.getUserManager().getUserByName("admin")
MutableIssue issue = (MutableIssue) event.issue;

log.warn("issueKey = "+ issue.key)

def cfRequestType = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("Customer Request Type")[0]

String issueType = issue.getIssueType().getName();

log.warn("issueType = "+ issueType)

String requestType = "";

switch(issueType){
case "Correctivo":
requestType = CORRECTIVO;
break;
}

log.warn("requestType = "+requestType)

log.warn( cfRequestType.getCustomFieldType().getSingularObjectFromString(requestType) )

def fieldRequestType = cfRequestType.getCustomFieldType().getSingularObjectFromString(requestType)
issue.setCustomFieldValue(cfRequestType, requestType)

ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

But is giving me this error:

2021-01-08 10:57:33,387 WARN [runner.ScriptBindingsManager]: issueKey = CPAMSSAP-133
2021-01-08 10:57:33,387 WARN [runner.ScriptBindingsManager]: issueType = Correctivo
2021-01-08 10:57:33,387 WARN [runner.ScriptBindingsManager]: requestType = Correctivo
2021-01-08 10:57:33,387 WARN [runner.ScriptBindingsManager]: 
2021-01-08 10:57:33,389 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2021-01-08 10:57:33,389 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: null
java.lang.ClassCastException: java.lang.String cannot be cast to com.atlassian.servicedesk.internal.customfields.origin.VpOrigin
	at com.atlassian.servicedesk.internal.customfields.origin.VpOriginCFType.getDbValueFromObject(VpOriginCFType.java:82)
	at com.atlassian.jira.issue.customfields.impl.AbstractSingleFieldType.createValue(AbstractSingleFieldType.java:143)
	at com.atlassian.jira.issue.fields.ImmutableCustomField.createValue(ImmutableCustomField.java:693)
	at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:410)
	at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:396)
	at com.atlassian.jira.issue.managers.DefaultIssueManager.updateFieldValues(DefaultIssueManager.java:728)
	at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:681)
	at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:667)
	at com.atlassian.jira.issue.managers.RequestCachingIssueManager.updateIssue(RequestCachingIssueManager.java:217)
	at com.atlassian.jira.issue.IssueManager$updateIssue$0.call(Unknown Source)
	at Script993.run(Script993.groovy:34)


Thanks in advance to everyone! 

1 answer

1 accepted

4 votes
Answer accepted
Noname person
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 10, 2021

Hi @Jorge Jerez 

Try this. It works for me at JSD v4.5.13

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.servicedesk.api.requesttype.RequestTypeService
import com.atlassian.servicedesk.api.request.type.CustomerRequestTypeService
import com.atlassian.servicedesk.api.user.UserFactory
import com.atlassian.servicedesk.api.util.paging.SimplePagedRequest
import com.atlassian.servicedesk.api.util.paging.PagedRequest
import com.atlassian.servicedesk.api.util.paging.PagedResponse
import com.atlassian.servicedesk.api.user.UserFactory
import com.atlassian.servicedesk.api.requesttype.RequestTypeQuery
import com.atlassian.servicedesk.api.requesttype.RequestType
import com.atlassian.servicedesk.api.user.UncheckedUser
import com.atlassian.servicedesk.api.request.type.CustomerRequestType
import com.atlassian.servicedesk.api.request.type.CustomerRequestTypeQuery
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.onresolve.scriptrunner.runner.customisers.PluginModule

@WithPlugin("com.atlassian.servicedesk")
@PluginModule
RequestTypeService requestTypeService
@PluginModule
CustomerRequestTypeService customerRequestTypeService
@PluginModule
UserFactory userFactory

String requestTypeName = "New employee"

ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() //Получаем текущего пользователя (создателя)
RequestType newRequestType
int numberOfRequestTypes = 300 //approximate amount of customer request types on your instance the more the better)
for(int limitRequest = 0;limitRequest <numberOfRequestTypes;limitRequest += 100){
PagedRequest pagedRequest = new SimplePagedRequest(limitRequest,limitRequest+100)
RequestTypeQuery reqTypeQuery = requestTypeService.newQueryBuilder().pagedRequest(pagedRequest).build()
PagedResponse<RequestType> reqTypes = requestTypeService.getRequestTypes(currentUser, reqTypeQuery)
if(reqTypes.results.find{it.getName()==requestTypeName}){
newRequestType = reqTypes.results.find{it.getName()==requestTypeName}
break
}
}
CustomerRequestTypeQuery customerRequestTypeQuery = customerRequestTypeService.newQueryBuilder().requestType(newRequestType).build()
UncheckedUser uncheckedUser = userFactory.getUncheckedUser()
CustomerRequestType customerRequestType = customerRequestTypeService.getCustomerRequestType(uncheckedUser, customerRequestTypeQuery) as CustomerRequestType

CustomField cfReqType = ComponentAccessor.getCustomFieldManager().getCustomFieldObjects(issue).find {it.name == 'Customer Request Type'}
issue.setCustomFieldValue(cfReqType , requestType)
ComponentAccessor.getIssueManager().updateIssue(currentUser , issue, EventDispatchOption.DO_NOT_DISPATCH, false)



toddler July 1, 2022

Very Nice thanks so much I was searching for a solution since hours.

BR

Heiko

Michael Okhotin March 29, 2023

Script above works perfectly, thanks. 
If you have a lot of different Customer Request types on your Jira instance, "numberofRequesttypes" number could be increased to 1000 to avoid any failures. 

Fabian February 12, 2024

Thx, it works perfectly.

I only notice one thing:

In the last line:

CustomField cfReqType = ComponentAccessor.getCustomFieldManager().getCustomFieldObjects(issue).find {it.name == 'Customer Request Type'}
issue.setCustomFieldValue(cfReqType , requestType)

the requestType must be the variable customerRequestType

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events