Forums

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

Script post function update field

Daniel Wellington February 26, 2019

Hi, I'm trying to work out why the below script isn't working. I'm trying to update two custom fields as part of a post function:

 

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.customfields.manager.OptionsManager

 

def optionsManager = ComponentAccessor.getComponent(OptionsManager)

def customFieldManager = ComponentAccessor.getCustomFieldManager()

 

def scrapCf = customFieldManager.getCustomFieldObjectByName("Country")

def fieldConfig = scrapCf.getRelevantConfig(issue)

def option = optionsManager.getOptions(fieldConfig).getOptionForValue("UK", null)

issue.setCustomFieldValue(scrapCf, option)

 

def scrapCf = customFieldManager.getCustomFieldObjectByName("City")

def fieldConfig = scrapCf.getRelevantConfig(issue)

def option = optionsManager.getOptions(fieldConfig).getOptionForValue("London", null)

issue.setCustomFieldValue(scrapCf, option)

 

checkAttachment = {attachment -> false};

issue.assigneeId = null;

 

1 answer

1 accepted

0 votes
Answer accepted
Antoine Berry
Community Champion
February 27, 2019

Hi Daniel,

I guess you are trying to update a Select list (single choice) custom field ?

This piece of code should do the trick : 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.index.IssueIndexingService

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def scrapCf = customFieldManager.getCustomFieldObjectByName("Country")
def currentValue = issue.getCustomFieldValue(scrapCf)
def fieldConfig = scrapCf.getRelevantConfig(issue)
def option = ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find { it.toString() == "UK" }
scrapCf.updateValue(null, issue, new ModifiedValue(currentValue, option), new DefaultIssueChangeHolder())
issue.store()
issueIndexingService.reIndex(issue)

Regards,

Antoine 

Suggest an answer

Log in or Sign up to answer