Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,360,004
Community Members
 
Community Events
168
Community Groups

Set Customer Request Type to Issue Type using Scriptrunner Listener

Edited

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

2 votes
Answer accepted

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)



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

BR

Heiko

Suggest an answer

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

Apps for Confluence you won't want to miss: RSVP for September's Appy Hours

Calling all collaborators and Confluence users! Our Appy Hours event on September 29th features 4 presenters demoing functionality to superpower Confluence. Don't miss learning about these apps i...

115 views 0 9
Read article

Atlassian Community Events