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

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

1 vote
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)



Suggest an answer

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

🍻🍂Apptoberfest Update: Upcoming Virtual Events 🎉

Hello Community! I hope you've been enjoying the 🍂Apptoberfestivities🍂 (I know I have!) The event is heating up next week with a series of virtual events that we're calling the 🍻🍂Partner App ...

553 views 5 19
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