Create an issue from a Scriptrunner Job with customfields

Joseph Fowler October 6, 2021

I have a scriptrunner job that  creates an issue based off of a JQL query result. All of that works  fine and the issue gets created. However, I want to be able to add a value to a custom field that is a radio button. I have use options manager and found the option and then use  setCustomFieldValue(cf, value) and it still is not liking it. I work in an environment where i can not share my code, if anyone has any tips that would greatly be appreciated. Also, I got around it by letting the job create the issue without the custom field and then have a listener wait for the creation of the issue and then i can update the value of the custom field. This is a workaround i want to get rid of because I need to make the custom field  required.

 

EDIT: Added code block below.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.search.SearchService
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.search.SearchQuery
import com.atlassian.jira.issue.context.IssueContext
import com.atlassian.jira.issue.context.IssueContextImpl
import com.atlassian.jira.issue.fields.config.manager.PrioritySchemeManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager

def user = ComponentsAccessor.jiraAuthentiationContext.loggedInUser
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchService = ComponentAccessor.getComponent(SearchService)
def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueService = CompnentAccessor.issueService
def constantsManager = ComponentAccessor.contanstsManager
def optionsManager = ComponentAccessor.getComponent(OptionsManager)

def jqlSearch = "project = TEST AND created >= startOfDay(-4h)
def query = jqlQueryParser.parseQuery(jqlSearch)

def results = searchService.search(user,query,PagerFilter.getUnlimitedFilter())
if (results == null) return

final projectKey = 'TEST'
final issueTypeName = 'Test Issue'
final reporterKey = 'admin'
final summary = 'Test Entry'
final description = 'Test Description'

def project = ComponentAccessor.projectManager.getProjectObjByKey(projectKey)
def issueType = constantsManager.allIssueTypeObjects.findByName(issueTypeName)
def issueContext = new IssueContextImpl(project, issueType) as IssueContext
def reporter = ComponentAccessor.userManager.getUSerByKey(reporterKey) ?: user

def cf = customFieldManager.getCustomFieldObject("customfield_10902")
def fieldConfig = cf.getRelevantConfig(issueContext)
def option = optionsManager.getOptions(fieldConfig).getOptionForValue("Yes", null)

def issueInputParameters = issueService.newIssueInputParameters();
issueInputParameters.setProjectId(project.id)
issueInputParameters.setIssueTypeId(issueType.id)
issueInputParameters.setReporterId(reporter.name)
issueInputParameters.setSummary(summary)
issueInputParameters.setDescription(description)
issueInputParameters.setCustomFieldValue("customfield_10902", option)
//issueInputParameters.addCustomFieldValue("customfield_10902", option)
//issueInputParameters.setCustomFieldValue("customfield_10902", "Yes")

def validationResult = issueService.validateCreate(user, issueInputParameters)
def result = issueService.create(user, validationResult)
return

 

 

 

2 answers

0 votes
Matti Dennstaedt May 5, 2022

Hi Joseph,

it needs the ID of the option. I just had the same problem. If you change your line as follows, it should work:

def option = optionsManager.getOptions(fieldConfig).getOptionForValue("Vorinstallation (Initial)", null).getOptionId()

def issueInputParameters = issueService.newIssueInputParameters();
issueInputParameters.setProjectId(project.id)
issueInputParameters.setIssueTypeId(issueType.id)
issueInputParameters.setReporterId(reporter.name)
issueInputParameters.setSummary(summary)
issueInputParameters.setDescription(description)
issueInputParameters.setCustomFieldValue("customfield_10902", option.toString())

 It is returned type long and must be converted to string.

0 votes
Will C
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.
October 7, 2021

it sounds like you are doing the right thing by updating this custom field using options manager, and if that works in a listener for this field it should work in a job, we would need to see your code to comment on where it is going wrong.

Make sure you are saving the value after you are setting it, but if you are doing that in a listener its the same in a job.

Joseph Fowler October 7, 2021

Im doing this in a job that runs once every night. I added the code block above of what I am doing. There may be some stuff in there thats not used anymore, but left in there from me trying all sorts of things to get custom field values in there.

Will C
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.
October 14, 2021

I'm not sure what the JQL search has to do with creating the task? Do you create a new task for every result the JQL returns?

I've tried to look at your script but there are a lot of errors in there when trying to run on the console, is this exactly what you are running?

Joseph Fowler October 21, 2021

Sorry i typed this up, I can't get the full code block. I work in a sensitive environment. This is just a gist of it from memory here. I create an issue if the search results returns no entries. Literally just want to create an issue in a job with custom fields that are select lists and radio buttons. Thats all i want to do

Suggest an answer

Log in or Sign up to answer