Script Runner - set Field "Customer Request type"

brainbits GmbH
Contributor
January 17, 2017

Is it possible to set the Field "Customer Request type" via a scriptrunner postfunction in a Service Desk Workflow?

3 answers

7 votes
Thanos Batagiannis [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.
January 23, 2017

Hi,

So Jonny is right saying that 'Service Desk fields can be a bit tricky' and his script is 100% correct for a single select list (as he did mention). But the Customer Request Type does not behave as a Single Select list. So ...

import com.atlassian.jira.component.ComponentAccessor

// In order to find the key of it select an request type for an issue and then navigate to
// http://<baseUrl>/rest/api/2/issue/<issueKey>?expand=renderedFields,editmeta
// and next to the id of the Customer Request Type custom field (see below in log) you will find it
def REPORT_SYSTEM_PROBLEM_KEY = "sdp/systemproblem"

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def tgtField = customFieldManager.getCustomFieldObjectByName("Customer Request Type")

// get the id of the cf in the logs in order to use it for finding the key of the Customer Request Type
log.debug("Customer Request Type Custom field Id: ${tgtField.getId()}")

def requestType = tgtField.getCustomFieldType().getSingularObjectFromString(REPORT_SYSTEM_PROBLEM_KEY)
issue.setCustomFieldValue(tgtField, requestType)

hope that helps, Thanos

David Klemme
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
July 7, 2017

if u stumble over this...

 

the request type in: 

issue.setCustomFieldValue(tgtField, requestType)

 

has to be the key NOT the id you grab from the rest api or the logs.

only the according entry to:

 

sdp/systemproblem

Btw, it seems that you do not have to be successful in setting a correct type.

After setCustomFiel..the request will show up in the customer portal, even if the type shows NULL in the issue itself..

Constance Hua
Contributor
April 11, 2018

@David Klemme didn't work for me ..

2 votes
Jonny Carter
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.
January 18, 2017

EDIT: See Thanos's answer, above. This is not quite right.

Service Desk fields can be a bit tricky, but I have seen examples of setting it via Behaviours (e.g. https://answers.atlassian.com/questions/15836926).

If it behaves like other fields in the context of Post Functions, then you could do something like:

import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.customFieldManager
def crtField = customFieldManager.getCustomFieldObjects(issue).find{it.name == "Customer Request Type"}
issue.setCustomFieldValue(crtField, "Desired Request Type")

 

It may be that the Customer Request Type behaves like a select list, in which case, you may need to bring in the Options manager.

import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.customFieldManager
def optionsManager = ComponentAccessor.optionsManager
def crtField = customFieldManager.getCustomFieldObjects(issue).find{it.name == "Customer Request Type"}
def fieldConfig = crtField.getRelevantConfig(issue)
def options = optionsManager.getOptions(fieldConfig)
def optionToPick = options.find{it.value == "Desired value"}
issue.setCustomFieldValue(crtField, optionToPick)

 

Yves Martin
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.
February 22, 2018

Hello. I tried to use that "select list options" code snippet with Customer Request Type field but it does not work.

Here are some ways to retrieve "project/UUID" expected values for this custom field:

- inspect jira page DOM to look for select option

- query database thanks to SQL query available at https://community.atlassian.com/t5/Jira-Service-Desk-questions/Re-set-customer-request-type-with-script-runner/qaq-p/120737/comment-id/1213#M1213

Dario B
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 2, 2019
1 vote
mleben April 5, 2023

Set Customer request field on create issue: 

 

This is how I did it. 

 
import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.IssueManager

import com.atlassian.jira.issue.MutableIssue

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

import com.atlassian.jira.issue.Issue

import org.apache.log4j.Logger

def logs = Logger.getLogger("com.acme.workflows")

logs.warn("Workflow function running...")
//set Customer request type

    def cfCustomerRequestType = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("Customer Request Type").first();

    final String SERVICE_REQUEST = "bsdr/5dd969ce-dcf6-46cd-9900-814174943441"

    def customerRequest = cfCustomerRequestType.getCustomFieldType().getSingularObjectFromString(SERVICE_REQUEST)

    logs.warn("customerRequest: " + customerRequest)

    IssueManager issueManager = ComponentAccessor.getIssueManager()

    MutableIssue issueToUpdate =  issueManager.getIssueObject(issue.getId()) as MutableIssue

    issueToUpdate.setCustomFieldValue(cfCustomerRequestType, customerRequest);

    issueManager.updateIssue(issue.getReporterUser(), issueToUpdate, EventDispatchOption.ISSUE_UPDATED, false);

In the variable, SERVICE_REQUEST set the UUID of your desired Customer Request type that you get from the database and it will work. 

 

Regards.

Sergey Kislitsyn May 30, 2023

Thank you, works like a charm

Query to get UUID of SERVICE_REQUEST

select stringvalue 
from customfieldvalue
where issue=(
select id from jiraissue where issuenum=123 and project=(select id from project where pkey='JRA')
)
and customfield=10909; 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events