How to Set Custom Field on Scriptrunner Scheduled Job

Jose Sepulveda April 26, 2021

Hello All,

I've been able to figure out how to create a scheduled job thanks to scriptrunner's documentation, however I cannot figure out how to set a custom field with it. Here's what I have so far:

import com.atlassian.jira.component.ComponentAccessor

def issueService = ComponentAccessor.issueService
def projectManager = ComponentAccessor.projectManager
def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()

def issueInputParameters = issueService.newIssueInputParameters()

issueInputParameters.with {
projectId = projectManager.getProjectObjByKey("IT").id
summary = "Monthly Storage Analysis"
issueTypeId = "10208"
reporterId = user.name
description = "Storage & Backup team,\nPlease provide the storage utilization details for backups of SQL DBs per domain that we will be using for our monthly report. For Elasticsearch, information regarding H5R domain is sufficient."
}

def validationResult = issueService.validateCreate(user, issueInputParameters)
assert !validationResult.errorCollection.hasAnyErrors()

def issueResult = issueService.create(user, validationResult)
log.info "Issue created: ${issueResult.issue}"

 I've found various pages on here that seem to answer this for other users but I haven't been able to get them to work.

For example, adding this leads to an error:

import com.atlassian.jira.event.type.EventDispatchOption

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManafer = ComponentAccessor.getIssueManager()
def cf = customFieldManager.getCustomFieldObjectByName("Domain(s)")

issue.setCustomFieldValue(cf,"All Domains")


So in the end, I just want to set the custom field "Domain(s)" as "All Domains"

3 answers

1 vote
Kate Kabir
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.
April 27, 2021

Hi @Jose Sepulveda 

Thank you for the question.

You are trying to use a server code in the cloud environment which will not work. Cloud uses the REST API's. We have an example here to set fields in our doc that you can use as an example here. Then, we have an example of how to update a checkbox field as your guide here

Hope this will help.

Kind Regards

Kate

Jose Sepulveda April 27, 2021

Hello @Kate Kabir

I'm not actually sure how to tell what Jira version we have. The only choice I had was cloud though I can't tell for sure if that's what we use. If I had to guess, it'd be closer to a Jira server as I know we do have a server set up with the internals of Jira.

That being said, I've used the first code block to create issues in our Jira in the past and it's worked fine using the scriptrunner scheduled job utility. It's just that I can't get custom fields to be set upon issue creation.

Kate Kabir
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.
April 28, 2021

Hi @Jose Sepulveda 

Thank you for the update.

I am sorry if my answer did not help you much. I believe it would be best if you can re-categorise the question as a server or create a new question for the server so that someone that is familiar with the server can assist you better.

Apologies for the inconvenience caused.

Thank you :)

Kind Regards

Kate

0 votes
Prince Nyeche
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.
April 27, 2021

I thought on cloud ScriptRunner uses Unirest library for REST API and not the Atlassian Java Library API?

0 votes
Kevin Johnson
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 26, 2021

Hi @Jose Sepulveda ,

Is the field that you're trying to set value is a Single line text field or Drop down or what it is exactly?

Cheers, Kevin

Jose Sepulveda April 26, 2021

Hello @Kevin Johnson

It's a checkbox type of field.

Like Kevin Johnson likes this

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events