Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Jira 5.1.8 How to set single select custom field using script runner?

Rakesh Katte September 13, 2020

Hi all,

I have a single select custom field that I am trying to set the value using a post function as below. But, the change does not reflect. I have tried doing multiple different things suggested on the blogs but nothing is working. Kindly, advice as to what I am missing.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.fields.config.FieldConfig
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

MutableIssue mutableIssue = (MutableIssue) issue
CustomField obj_releaseType = customFieldManager.getCustomFieldObjectByName('Release Type')
FieldConfig fieldConfigRT = obj_releaseType.getRelevantConfig(issue)
Option value = ComponentAccessor.getOptionsManager().getOptions(fieldConfigRT)?.find { it.toString() == 'Test - Internal'}

if(value)obj_releaseType.updateValue(null, mutableIssue,new ModifiedValue(mutableIssue.getCustomFieldValue(obj_releaseType), value),new DefaultIssueChangeHolder())


issueManager.updateIssue(issue.getReporter(), mutableIssue, EventDispatchOption.ISSUE_UPDATED, false)

 

Instead of using the updateValue function I have also tried using the below function.

mutableIssue.setCustomFieldValue(obj_releaseType, value)

 

1 answer

1 accepted

0 votes
Answer accepted
Gustavo Félix
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 13, 2020

Hi @Rakesh Katte 

if(rtvalue)obj_releaseType.updateValue(null, mutableIssue,new 

"rtvalue" doesn't exist on your code, I guess is "value". 

I don't know if this is a typo only on this code or also on your original code.

Rakesh Katte September 13, 2020

Oops!! Just a typo while adding the question to the blog. Corrected it now. Thanks for pointing out.

Gustavo Félix
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 13, 2020

I think your code seems ok. 
In what order is your post function ? 

On the screen where you add the post function, there is:

1.-Creates the issue originally
2.- Re index an issue ....
3.- Fire event

Rakesh Katte September 13, 2020

It's the first one in the below list of steps:

Scripted post function

Set issue status to the linked status of the destination workflow step.

Add a comment to an issue if one is entered during a transition.

Update change history for an issue and store the issue in the database.

Re-index an issue to keep indexes in sync with the database.

Fire a Generic Event event that can be processed by the listeners.
Edit

Gustavo Félix
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 13, 2020

Can you try on the 4th place?

Rakesh Katte September 13, 2020

Still nothing. I have changed the order to the below:

Set issue status to the linked status of the destination workflow step.

Add a comment to an issue if one is entered during a transition.

Update change history for an issue and store the issue in the database.

Scripted post function

Re-index an issue to keep indexes in sync with the database.

Fire a Generic Event event that can be processed by the listeners.

Gustavo Félix
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 13, 2020

I dont have all those , Im not sure why .

The way I do it is "Add post function" then "Script Post-Function[Script Runner] "

Then, my order is  :
Create the issue originally
Re index
Fire a Issue Created
And then I add my script. 

So , you can try to add it on the last position just to discard that.

Another thing I would do is
log.warn ComponentAccessor.getOptionsManager().getOptions(fieldConfigRT)
to see the options that are available on your select, just to be sure it is reading it correctly.


Rakesh Katte September 14, 2020

It turned out to be a missing import :( in my actual code. Since I don't have access to the filesystem where Jira is deployed, i don't get to see the logs easily. Hence, the goof up. 

@Gustavo Félix Apologize for wasting your time. But, thanks much for looking into this.

Suggest an answer

Log in or Sign up to answer