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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

359 views 0 6
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