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

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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Jul 10, 2018 in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

175 views 0 4
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