Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,297,444
Community Members
 
Community Events
165
Community Groups

Create an issue from a Scriptrunner Job with customfields

Edited

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

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.

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.

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.

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?

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
TAGS
Community showcase
Published in Jira Software

Upcoming changes to epic fields in company-managed projects

👋 Hi there Jira Community! A few months ago we shared with you plans around renaming epics in your company-managed projects. As part of these changes, we highlighted upcoming changes to epics on...

14,731 views 37 47
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you