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,297,452
Community Members
 
Community Events
165
Community Groups

Creating new Requests in a different Service Desk (Scriptrunner)

The need to create Requests in different Service Desks has grown over the last few months. I used to follow these steps: 

1. Add a script to a workflow post-function that creates the ticket in the other service desk (using "issueService")

2. Add another script to the workflow on the other Service Desk to add a "Customer Request Type" (using issue.setCustomFieldValue([Customer Request Type Field], [Customer Request Type Id]), where the Id looked something like this: key/kjjfsd03kjs03jklj1293ckjf)

The "problem" is that I don't want to do step 2 for EVERY SINGLE request that I create in a different Service Desk. I would rather include the "Customer Request Type" in step 1. This will eliminate the need to always build in "defaults" or logic that adds the correct Customer Request Type. 

Here is my attempt at including the "Customer Request Type" with the issueInputParameters (removed code that probably isn't relevant): 

def IssueInputParameters issueInputParameters = issueService.newIssueInputParameters();

issueInputParameters.addCustomFieldValue(customer_request_type.getId(), customer_request_type_key.toString())

def validationResult = issueService.validateCreate(loggedInUser, issueInputParameters)
def issueResult = issueService.create(loggedInUser, validationResult)

I have tried a few different customer_request_type_key values: 

  • key/kjjfsd03kjs03jklj1293ckjf
  • 117 (the ID of the request)
  • "Office Move" (the name of the request which I didn't expect to work but tried anyways)

The first one works every single time I use it with issue.setCustomFieldValue(), which almost always occurs on the create transition.

I have also attempted to do a issueService.validateUpdate() and issueService.update(). But I get the same result...still an empty Customer Request Type. I suspect the problem comes from IssueInputParameters. I couldn't find any other method besides addCustomFieldValue that made sense to use. 

I also attempted to just do issueResult.getIssue().setCustomFieldValue() in the same script as above, but it never is successful. 

Any help/tips would be greatly appreciated. 

2 answers

1 accepted

0 votes
Answer accepted

For those interested, this is what I eventually did: 

  1. Create a new Custom Field called "Customer Request Key"
  2.  Setup a Script Listener (using Scriptrunner) that runs on the "Issue Created" event. 
  3. Script looks like what is found at the bottom.

The cf_Service is a separate script that makes it slightly easier to grab the objects and values of a specific custom field. 

 

The nice thing about doing it this way is that I can send that Customer Request Key anywhere and in any way, and it'll update the Customer Request Type of the issue. This solved my problem which was constantly needing to write a script for the sending Service Desk AND the receiving Service Desk EVERY SINGLE time I was creating a ticket in another Service Desk. 

 

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.event.type.EventDispatchOption

import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

import com.atlassian.jira.issue.ModifiedValue

GroovyShell shell = new GroovyShell()

def cf_Service = shell.parse(new File('./scripts/jira_scripts/Issue_Services/cf_Service.groovy'))

//User that will update the issue.
def
jiraAdminUser = ComponentAccessor.getUserManager().getUserByName('jira-localadmin')

//Get Customer Request Type and Customer Request Key objects.
def requestType = cf_Service.getCF_getValue(issue, "Customer Request Type")
def crtKey = cf_Service.getCF_getValue(issue, "Customer Request Key")

//Check if Customer Request Key has a value
if (crtKey.value) {

//Converts Customer Request Key (which is a string) to an actual Request Type object
    def newRequestKey = requestType.cf.getCustomFieldType().getSingularObjectFromString(crtKey.value)

    def modifiedValue = new ModifiedValue(null, newRequestKey)

//Update the value of the request type on the ticket
    requestType.cf.updateValue(null, issue, modifiedValue, new DefaultIssueChangeHolder())

    def deleteKey = new ModifiedValue(null, "")

//Delete the Customer Request Key value on the ticket
    crtKey.cf.updateValue(null, issue, deleteKey, new DefaultIssueChangeHolder())

//Actually update the ticket
    ComponentAccessor.issueManager.updateIssue(jiraAdminUser, issue, EventDispatchOption.ISSUE_UPDATED, false /*sendMail*/)

}

Hi @wesley_geddes

Have you tried passing the request type object itself?

You can figure out how to get the object by referring to this script: Get the Request Type Name in an Issue Event Listener

Cheers,
Helmy

@Helmy Ibrahim _Adaptavist_ 

Yes, but the IssueInputParameters.addCustomFieldValue(Long, String) requires that I provide the customFieldID, and a String value. 

I followed the script you provided and get this from the "requestType" variable (log.warn(request type object - request type class)): requestType: com.atlassian.servicedesk.internal.feature.customer.request.requesttype.CachedImmutableRequestTypeImpl@20b64a71 - class com.atlassian.servicedesk.internal.feature.customer.request.requesttype.CachedImmutableRequestTypeImpl

If I spit out the object as a string all I get is "com.atlassian.servicedesk.internal.feature.customer.request.requesttype.CachedImmutableRequestTypeImpl@20b64a71" which doesn't work. Thoughts? 

 

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira Service Management

Coming Soon: Insight Changing to Assets

The 2020 acquisition of Mindville added powerful asset and configuration management capabilities to Jira Service Management in the form of Insight. Following the completion of that integration, custo...

117 views 0 4
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