Need to update the value of a dropdown customfield

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

This widget could not be displayed.

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)
}

Thanks! That worked!!

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
Community showcase
Published Aug 22, 2018 in Marketplace Apps

How a Marketplace app tech team is achieving gender diversity

Hello! My name is Genevieve Blanch, and I'm the Marketing Manager at RefinedWiki, creators of apps to give teams the tools to customize Atlassian platforms. Currently, 44% of the tech team at Re...

485 views 3 18
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you