Removing a value from a multi-select custom field in a post function

Roy Chapman March 18, 2019

Does anyone have an example script that can be used in a post function to remove a value from a multi-select custom field? 

1 answer

1 accepted

0 votes
Answer accepted
Ravi Sagar _Sparxsys_
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.
March 18, 2019
Roy Chapman March 19, 2019

@Ravi Sagar _Sparxsys_ 

Thanks. That put me in the right direction.  I have implemented this script.

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.CustomFieldManager
import org.apache.log4j.Logger
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue

def issue = issue as MutableIssue

def log = Logger.getLogger("com.acme.workflows")

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();

def approvalGiven = customFieldManager.getCustomFieldObjectByName("Approval Given")
def approvalGivenVal = issue.getCustomFieldValue(approvalGiven) as ArrayList

def optionsManager = ComponentAccessor.getOptionsManager()
def optionTech = optionsManager.getOptions(approvalGiven.getRelevantConfig(issue)).find {it.value == "Tech"}
def optionBusiness = optionsManager.getOptions(approvalGiven.getRelevantConfig(issue)).find {it.value == "Business"}
def optionRelease = optionsManager.getOptions(approvalGiven.getRelevantConfig(issue)).find {it.value == "Release"}

if (approvalGivenVal?.contains(optionRelease)) {
log.warn "Approval Given is set to " + optionRelease
approvalGivenVal.remove(optionRelease)
}

def changeHolder = new DefaultIssueChangeHolder()
approvalGiven.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(approvalGiven), approvalGivenVal),changeHolder)

(The script is used in 3 places, hence the unused values optionTech and optionBusiness)

Thanks again

Ravi Sagar _Sparxsys_
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.
March 25, 2019

That's great. Thanks for sharing here as well.

Suggest an answer

Log in or Sign up to answer