Need to update the value of a dropdown customfield

Praveen July 24, 2017

Hi, I am trying to update a customfield option (dropdown) to "Inactive" option upon a certain condition.  I tried the below code and executed it as a postfunction script but it is not working.  The code dosenot give any errors but the field option is not updating. The code is basically to update the state of linked child issues to "Inactive"

 

import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.project.Project
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.util.ErrorCollection
import com.atlassian.jira.user.*
import com.atlassian.jira.bc.issue.IssueService.TransitionValidationResult
import com.atlassian.jira.issue.IssueInputParametersImpl


def issue = event.issue as issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def issueId = issue.getId()
def issueManager = new ComponentAccessor().getIssueManager()
def issueLink = issueLinkManager.getOutwardLinks(issueId)

for(int j=0; j<issueLink.size(); j++){
def linkedIssueId = issueLink[j].getDestinationId()
def linkedIssueName = issueManager.getIssueObject(linkedIssueId)
def issueName = linkedIssueName
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("State")
def cfConfig = cf.getRelevantConfig(issueName)
def value = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {
it.toString() == 'Inactive' }

issueName.setCustomFieldValue(cf, value)
}

  

1 answer

1 vote
Joshua Yamdogo @ Adaptavist
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.
July 24, 2017

Hi Praveen,

 

Try using the updateValue method to actually make sure the field's value gets changed:

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

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def issueId = issue.getId()
def issueManager = new ComponentAccessor().getIssueManager()
def issueLink = issueLinkManager.getOutwardLinks(issueId)

for(int j=0; j<issueLink.size(); j++){
def linkedIssueId = issueLink[j].getDestinationId()
def linkedIssueName = issueManager.getIssueObject(linkedIssueId)
def cf = customFieldManager.getCustomFieldObjects(linkedIssueName).find {it.name == "State"}
def cfConfig = cf.getRelevantConfig(linkedIssueName)
def value = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {
it.toString() == 'Inactive' }

def changeHolder = new DefaultIssueChangeHolder()
cf.updateValue(null, linkedIssueName, new ModifiedValue(issue.getCustomFieldValue(cf), value), changeHolder)
}
Praveen July 24, 2017

Thanks! That worked!!

Joshua Yamdogo @ Adaptavist
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.
July 25, 2017

Glad that it worked for you! By the way, you can select my answer as the Accepted Answer so that others can more easily see the solution if they have the same question. 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events