Can't change the field value 'Customer request type' by ScriptRunner
Asked by Stumper Dumpel:

Hi Dear Community,  

I had a problem again after the update, now with JSM. After upgrading to version 5.12.10, the script that sets the value of the 'Customer request type' field stopped working correctly.

On some projects, for some reason the field value just stopped being set.

In other cases it is set, but in the task it is displayed as 'No match'. Maybe someone has encountered this problem and knows how to solve it?

<br />

![\[ES-6494\] Test issue 1.png](/t5/image/serverpage/image-id/336438iB67D7A627091C32F/image-size/large?v=v2&px=999 "[ES-6494] Test issue 1.png")  

<br />

To set the value, I use:  

<br />

And if you go to the portal, the request type is set correctly.

```
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder import com.atlassian.jira.issue.ModifiedValue 
import com.atlassian.jira.component.ComponentAccessor 
import com.atlassian.jira.event.type.EventDispatchOption 
import com.atlassian.jira.issue.MutableIssue
 
def issue = ...

//issue.requestType 

def customerReqTypeCF = ComponentAccessor.CustomFieldManager.getCustomFieldObject("customfield_10000") 
def key = "es/8513f0d8-16c3-4a36-a41e-73ad733334b2" 
def newCustomerReqType = customerReqTypeCF.getCustomFieldType().getSingularObjectFromString(key) 
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

customerReqTypeCF.updateValue(null, issue, new ModifiedValue("", newCustomerReqType), new DefaultIssueChangeHolder()) 
ComponentAccessor.issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, true)
```

<br />

UPD.

The problem is that when I set the field value manually and then retrieve the field value by scriptrunner, I get the value 'es/8513f0d8-16c3-4a36-a41e-73ad733334b2'. But if I take a value from another task and set it in test task, I get the field value 'engsup/8513f0d8-16c3-4a36-a41e-73ad733334b2'. It turns out that the value is broken because of a project key that was changed earlier.  
![?????? ?????? 2024-07-23 112459.png](https://community.atlassian.com/t5/image/serverpage/image-id/337254iFD6A8A9829CB871F/image-size/large?v=v2&px=999 "?????? ?????? 2024-07-23 112459.png")


Answers:
author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Stumper Dumpel](/t5/user/viewprofilepage/user-id/5221754)

Given what you are encountering, I suggest you change your approach to [ScriptRunner's HAPI](https://docs.adaptavist.com/sr4js/latest/hapi/work-with-jira-service-management) feature and ScriptRunner's **Custom Script Post-Function**.

Below is a sample working code for your reference:-  

```
issue.set {
    if (issue.issueType.name == 'Bug') {
        setRequestType('Report a bug')
    } else if (issue.issueType.name == 'Support') {
        setRequestType('Technical support')
    } else if (issue.issueType.name == 'New Feature')  {
        setRequestType('Suggest a new feature')
    }
}
```

Please note that the sample working code above is not 100% exact to your environment. Hence, you must make the required modifications.

Below are the screenshots of the Post-Function configuration.

1. Use the Create or whichever transition you want to add the Post-Function:-

![post_function1.png](https://community.atlassian.com/t5/image/serverpage/image-id/337017i992EB46F0E35C85F/image-size/large?v=v2&px=999 "post_function1.png")

2. Select ScriptRunner's Custom Script Post-Function

![post_function2.png](https://community.atlassian.com/t5/image/serverpage/image-id/337018iCD1044C6125433E4/image-size/large?v=v2&px=999 "post_function2.png")

3. Add the sample code I have provided above and modify it according to your requirements.

![post_function3.png](https://community.atlassian.com/t5/image/serverpage/image-id/337019i814AAABC825FFF40/image-size/large?v=v2&px=999 "post_function3.png")

So, in my environment, using the approach above, whenever I create a ticket via the standard Jira interface, the Request type automatically get's updated accordingly.

I hope this helps to solve your issue. :-)  

Thank you and Kind regards,  
Ram

comments: author: Stumper Dumpel
body: Hi. Thank you so much for your help!  
I found what the problem I had, I described it in my first post.


author: Adolfo Casari
body: [@Stumper Dumpel](/t5/user/viewprofilepage/user-id/5221754)Can you run that script from the console for an individual issue and print the error you get?

You mention that this starts showing up after a JSM upgrade, have you upgraded to the latest version of Scriptrunner?

comments: author: Stumper Dumpel
body: I'm already running this script in the console.  
SR version 8.30.0.  

I also noticed that if I take the value of the 'customer request type' field from another task created through the portal, the correct value is set in the test task.  

![?????? ?????? 2024-07-19 092053.png](https://community.atlassian.com/t5/image/serverpage/image-id/336598i038110AEF0AE31A0/image-size/large?v=v2&px=999 "?????? ?????? 2024-07-19 092053.png")  

My guess is that the conversion from string to object stopped working.

```
def key = "es/8513f0d8-16c3-4a36-a41e-73ad733334b2" 
def newCustomerReqType = customerReqTypeCF.getCustomFieldType().getSingularObjectFromString(key) 
```


author: Joseph Chung Yin
body: [@Stumper Dumpel](/t5/user/viewprofilepage/user-id/5221754) -

Did you check to ensure that the Request Type is valid for your JSM project? For example that you can create the specific request type from the portal or find the value in the dropdown list in the project's issue UI?

Please advise.

Best, Joseph Chung Yin

comments: author: Stumper Dumpel
body: Hi!  
Yes, I am using a certain type of request type and a certain type of task as a test.  
Requst type in project settings:  
![Edit Request Type Group - Service project.png](https://community.atlassian.com/t5/image/serverpage/image-id/336454iF3DA4773B5C8DDDA/image-size/large?v=v2&px=999 "Edit Request Type Group - Service project.png")

Test issue type:  
![Monosnap \[ES-6494\] Test issue 1.png](https://community.atlassian.com/t5/image/serverpage/image-id/336456i67FB1C1BF0C38FBF/image-size/large?v=v2&px=999 "Monosnap [ES-6494] Test issue 1.png")  

Available request type:  
![123 2024-07-18 2.png](https://community.atlassian.com/t5/image/serverpage/image-id/336457i88D563AE31FD6CCB/image-size/large?v=v2&px=999 "123 2024-07-18 2.png")  
Request type key and name in SR console:  
![Chrome 2024-07-18.png](https://community.atlassian.com/t5/image/serverpage/image-id/336458i325F3A5B692085CC/image-size/large?v=v2&px=999 "Chrome 2024-07-18.png")


Tags: data-center

jira-service-management

jira-service-management-data-center

scriptrunner