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

Script for selecting a valid Customer Request Type on an Issue

Normann P_ Nielsen _Netic_
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.
July 20, 2022

One of the many lacks in finishing up the JSM product properly from Atlassian, is that when users create an Issue from Jira in JSM (and I know Atlassian wants us to raise a ticket on the left...), no Customer Request Type is added, so Customer cant see the ticket.

 

This Scripts loops possible Customer Request Types and sets one. I have it on the Create Step Post-fuction...

 

 


import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.servicedesk.api.portal.Portal
import com.atlassian.servicedesk.api.portal.PortalService
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.servicedesk.api.requesttype.RequestTypeService
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.project.type.ProjectTypeKeys

String issueKey = (String)issue.key
String issueType = (String)issue.getIssueType().name
String projectKey = (String)issue.getProjectObject().getKey()
int portalId = 0

Random random = new Random()
String scriptRunIdent = Math.abs(random.nextInt() % 99999 ) +1
String scriptName = "SetCustomerRequestType.groovy"

log.setLevel(org.apache.log4j.Level.INFO)
log.info "Script=" + scriptName + " IssueKey=" + issueKey + " ProjectKey=" + projectKey + " ScriptRunIdent=" + scriptRunIdent + " Action=StartScript"

def portalManager = ComponentAccessor.getOSGiComponentInstanceOfType(PortalService)
def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)

ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
ApplicationUser automationUser = ComponentAccessor.getUserManager().getUserByKey("automation")

if (issue.getProjectObject().getProjectTypeKey().getKey() == "service_desk" && !issue.isSubTask())
{
Portal targetPortalResult = portalManager.getPortalForProject(currentUser, issue.getProjectObject())
portalId = targetPortalResult.getId()

if (portalId > 0)
{
log.info "Script=" + scriptName + " IssueKey=" + issueKey + " ProjectKey=" + projectKey + " ScriptRunIdent=" + scriptRunIdent + " Message='Portal Id:" + portalId + "'"

def requestTypeService = ComponentAccessor.getOSGiComponentInstanceOfType(RequestTypeService)
def reqQ = requestTypeService.newQueryBuilder().serviceDesk(portalId).build()
def reqT = requestTypeService.getRequestTypes(automationUser, reqQ)

def requestTypes = [:]
def requestType = reqT.results.toArray()

boolean issueUpdated = false
def crtField = customFieldManager.getCustomFieldObjects(issue).find{it.name == "Customer Request Type"}
requestType.each { theType ->

log.info "Script=" + scriptName + " IssueKey=" + issueKey + " ProjectKey=" + projectKey + " ScriptRunIdent=" + scriptRunIdent + " Message='Request Type: " + theType.getIssueTypeId() + "'"

if (theType.getIssueTypeId().toString() == issue.getIssueTypeId().toString() && !issueUpdated)
{

log.info "Script=" + scriptName + " IssueKey=" + issueKey + " ProjectKey=" + projectKey + " ScriptRunIdent=" + scriptRunIdent + " Message='IssueType Match'"

//Update the Customer Request Field:
MutableIssue mIssue = issueManager.getIssueByCurrentKey(issue.getKey())

def requestTypeKey = "${issue.projectObject.key.toLowerCase()}/${theType.key}"
def newValue = crtField.getCustomFieldType().getSingularObjectFromString(requestTypeKey)
mIssue.setCustomFieldValue(crtField,newValue)
issueManager.updateIssue(automationUser, mIssue, EventDispatchOption.DO_NOT_DISPATCH, false)

log.info "Script=" + scriptName + " IssueKey=" + issueKey + " ProjectKey=" + projectKey + " ScriptRunIdent=" + scriptRunIdent + " Message='Issue Updated'"

boolean wasIndexing = ImportUtils.isIndexIssues()
ImportUtils.setIndexIssues(true)
issueIndexingService.reIndex(issueManager.getIssueObject(mIssue.id))
ImportUtils.setIndexIssues(wasIndexing)

log.info "Script=" + scriptName + " IssueKey=" + issueKey + " ProjectKey=" + projectKey + " ScriptRunIdent=" + scriptRunIdent + " Message='Issue Reindexed'"

issueUpdated = true
}
}
}
}
else
{
log.info "Script=" + scriptName + " IssueKey=" + issueKey + " ProjectKey=" + projectKey + " ScriptRunIdent=" + scriptRunIdent + " Message='Not a Service Management Project'"
}

log.info "Script=" + scriptName + " IssueKey=" + issueKey + " ProjectKey=" + projectKey + " ScriptRunIdent=" + scriptRunIdent + " Action=EndScript"

And Thanks to Adaptavist Support (exellent as ever) for a little help with it...
PS: The logging stuff can be removed, its just my custom way of logging from groovy..

0 comments

Comment

Log in or Sign up to comment
TAGS
AUG Leaders

Atlassian Community Events