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,465,462
Community Members
 
Community Events
176
Community Groups

Script for selecting a valid Customer Request Type on an Issue

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

Atlassian Community Events