Forums

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

set value of custom field in scriptrunner scheduled job

David Howe
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
May 1, 2021

I'm using Jira server ver 8.13.3

I have a text based custom field called "Deliverables" and I'm trying to create a Custom scheduled job to create an issuetype story every day with the "Deliverables" field set to "insert custom text". 

Deliverables = customfield_13518

I've tried the following but it never actually injects the text into the custom field

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = Component.Accessor.customFieldManager()
def issueService = ComponentAccessor.issueService
def projectManager = ComponentAccessor.projectManager
def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()

def issueInputParameters = issueService.newIssueInputParameters()

issueInputParameters.with {
projectId = projectManager.getProjectObjByKey("PROJ").id
summary = "reoccurring task"
issueTypeId = "21"
reporterId = user.name
description = "Daily Task Checklist"
customfield_13518 = "Submit Form 1362"
}

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

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

1 answer

0 votes
Vikrant Yadav
Community Champion
May 1, 2021

Hi @David Howe Try to insert value in custom field using below code :- 

issueInputParameters.addCustomFieldValue(cf.idAsLong, requestTypeValue)
Vikrant Yadav
Community Champion
May 1, 2021

Hi @David Howe  :- Working Code for me :- 

 

import com.atlassian.jira.component.ComponentAccessor

def issueService = ComponentAccessor.issueService
def projectManager = ComponentAccessor.projectManager
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def requestTypeValue = "Submit Form 1362" //replace it with the one you want - see note below

def constantsManager = ComponentAccessor.getConstantsManager()
def issueType = constantsManager.getAllIssueTypeObjects().find {it.name.equalsIgnoreCase("New Project Request")}
def issueInputParameters = issueService.newIssueInputParameters()
def customFiledmanager = ComponentAccessor.getCustomFieldManager()
def cf = customFiledmanager.getCustomFieldObjectByName("CA Ticket#")

issueInputParameters.with {
projectId = projectManager.getProjectObjByKey("CJS").id
summary = "Issue created from script"
issueTypeId = issueType.getId()
reporterId = "vikrant-yadav2"
assigneeId = "vikrant-yadav2"
}

issueInputParameters.addCustomFieldValue(cf.idAsLong, requestTypeValue)
def validationResult = issueService.validateCreate(user, issueInputParameters)
assert !validationResult.errorCollection.hasAnyErrors()

def issueResult = issueService.create(user, validationResult)

assert !issueResult.errorCollection.hasAnyErrors()
return "Issue created: ${issueResult.issue}"

Suggest an answer

Log in or Sign up to answer