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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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,559,795
Community Members
 
Community Events
185
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

3 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

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. 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events