How set Custom field value with scriptrunner (Server)

Alex Kulichkov July 26, 2021

Hello,

 

We are trying to set custom field value(s) for all issues created in a project. 

We don't want to use postfunction, since workflow is shared, with other projects  and we want this solution to be scalable, instead of creating a copy of a workflow for every project.

 

Tried to set up custom listener for particular project with following code, but it keeps throwing errors in the console:

import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjectByName("customFieldId=21004")
def cfConfig = cf.getRelevantConfig(issue)
value = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find { it.toString() == 'OTECH' }
issue.setCustomFieldValue(cf, value)

 

 

I usually don't use scriprunner, but in this particular instance its the only plug in available.

Any help is appreciated! 

1 answer

1 accepted

0 votes
Answer accepted
Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 27, 2021

Hi @Alex Kulichkov ,

this line doesn't seem right:

def cf = customFieldManager.getCustomFieldObjectByName("customFieldId=21004")

I would recommend to replace it with:

def cf = customFieldManager.getCustomFieldObject(21004)

Please check, you are really able to get the custom field object.

Also I believe setting custom field value is not enough, you should also store the value to persist the change to the database.

Alex Kulichkov July 27, 2021

Thank you Hana,

 

We fixed the script to be used in combination with behavior, it worked and looks like that.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager

// new try
def optionsManager = ComponentAccessor.getOptionsManager()
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(21004)
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)
def optionToSelectAI = options.find {it.value == "OTECH"}

// mod
getFieldById("customfield_21004").setFormValue(optionToSelectAI.optionId)
getFieldById("customfield_21004").setFieldOptions(options.findAll {it.value in ['OTECH']})

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events