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

Creating new Requests in a different Service Desk (Scriptrunner)

wesley_geddes September 2, 2021

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

1 vote
Answer accepted
wesley_geddes March 17, 2022

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*/)

}
1 vote
Helmy Ibrahim _Adaptavist_
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.
September 3, 2021

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

wesley_geddes September 7, 2021

@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
AUG Leaders

Atlassian Community Events