Forums

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

update custom filed by Custom script post-function

jun lee
Contributor
June 16, 2022

Hello

 

I would like to update custom filed by Custom script post-function  and my script is like below.

 

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

def customFieldManager = ComponentAccessor.customFieldManager
def escalate = customFieldManager.getCustomFieldObjectsByName("DeployApprove").first()


def optionsManager = ComponentAccessor.getOptionsManager()
def config = escalate.getRelevantConfig(issue)
def options = optionsManager.getOptions(config)
def optionToSelect = options.find {
it.value == "yes"
}
escalate.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(escalate), optionToSelect), new DefaultIssueChangeHolder())

 

However, this is failed when the custom filed "DeployApprove" is checked "No" already before this script works.

 

How should I change this script to be changed when the custom field is null or checked "No"?

 

1 answer

0 votes
padraik
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.
June 21, 2022

Give this a shot

 

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue

def cfManager = ComponentAccessor.customFieldManager
def issueManager = ComponentAccessor.issueManager
def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService.class)

def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def cfSelect = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("DeployApprove")
def cfConfig = cfSelect.getRelevantConfig(issue)
def value = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {it.toString() == 'Yes'}
issue.setCustomFieldValue(cfSelect, value)

issueManager.updateIssue(currentUser, issue, EventDispatchOption.ISSUE_UPDATED, false)

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events