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)
}
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)
}
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.