I am trying to set the custom field's value by calling setCustomFieldvalue(CUSTOM_FIELD , OPTION_ID)
But since issue has no setters, we converted it into mutable issue to call above function. As mutable issue is like an instance of the original issue. The above function is not changing the custom field's value.
Can anyone suggest me any solution for this.
Hello @divya krishna
You have an error here
def OPTION_1Value = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {
it.toString() == 'OPTION 1'
}.getOptionId()
Correct way to get Option
def OPTION_1Value = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {
it.value == 'OPTION 1'
}.getOptionId()
@Mark Markov We tried your suggested method but still without any errors in logs, the custom field value is unchanged even though the status is changed.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
In setCustomFieldValue for options field, you must pass Option as Value not OptionId.
Is this script listener?
Try like this
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManger =ComponentAccessor.getOptionsManager()
def issue = issueManager.getIssueObject(event.issue.key)
def customField = customFieldManager.getCustomFieldObjectsByName("Your options field name").first()
def config = customField.getRelevantConfig(issue)
def option = optionsManger.getOptions(config).find {it.value == "Options value"}
issue.setCustomFieldValue(customField, option)
issueManager.updateIssue(event.getUser(), issue, EventDispatchOption.ISSUE_UPDATED, false)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.bc.issue.IssueService
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
import com.opensymphony.workflow.InvalidInputException
import org.apache.log4j.Category
import com.atlassian.jira.workflow.WorkflowManager
import com.atlassian.jira.workflow.JiraWorkflow
import com.atlassian.jira.config.ConstantsManager
def issue = event.issue as Issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def issueService = ComponentAccessor.getIssueService() as IssueService
def user = ComponentAccessor.getUserManager().getUserByName('Name')
def customField = customFieldManager.getCustomFieldObjectByName("Resolution")
def issueStatus=issue.getStatus().getName()
def cfConfig = customField.getRelevantConfig(issue)
def OptionValue1 = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {
it.value == 'Unresolved'
}
def OptionValue2 = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {
it.value == 'Done'
}
def issueToUpdate = (MutableIssue) issue
if(issueStatus.equals("Draft")||issueStatus.equals("Prescreening")){
issueToUpdate.setCustomFieldValue(customField, OptionValue1)
issueManager.updateIssue(event.getUser(), issueToUpdate, EventDispatchOption.ISSUE_UPDATED, false)
}
else if(issueStatus.equals("Approved")||issueStatus.equals("Rejected")||issueStatus.equals("Obsoleted")){
issueToUpdate.setCustomFieldValue(customField, OptionValue2)
issueManager.updateIssue(event.getUser(), issueToUpdate, EventDispatchOption.ISSUE_UPDATED, false)
}
@Mark Markov But its status not getting change
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Mark Markov Similar code i have write in script listener but its status not changing Could you please help me on this
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Resolution field is a system field, not Custom. You should use method setResolution of mutableIssue object. Like this
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
def issue = event.issue as Issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getUserManager().getUserByName('Name')
def issueStatus=issue.getStatus().getName()
def OptionValue2 = ComponentAccessor.getConstantsManager().getResolutions().find {it.name == "Done"}
def issueToUpdate = (MutableIssue) issue
if(issueStatus.equals("Draft")||issueStatus.equals("Prescreening")){
issueToUpdate.setResolution(null)
issueManager.updateIssue(event.getUser(), issueToUpdate, EventDispatchOption.ISSUE_UPDATED, false)
}
else if(issueStatus.equals("Approved")||issueStatus.equals("Rejected")||issueStatus.equals("Obsoleted")){
issueToUpdate.setResolution(OptionValue2)
issueManager.updateIssue(event.getUser(), issueToUpdate, EventDispatchOption.ISSUE_UPDATED, false)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Mark Markov I have create separate custom field for resolution then also its status is not changing
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Then try this. And on what events do you set in this custom listener?
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.event.type.EventDispatchOption
def issue = event.issue as Issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def customField = customFieldManager.getCustomFieldObjectByName("Resolution")
def issueStatus=issue.getStatus().getName()
def cfConfig = customField.getRelevantConfig(issue)
def OptionValue1 = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {
it.value == 'Unresolved'
}
def OptionValue2 = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {
it.value == 'Done'
}
def issueToUpdate = issueManager.getIssueObject(issue.getKey())
if(issueStatus.equals("Draft")||issueStatus.equals("Prescreening")){
issueToUpdate.setCustomFieldValue(customField, OptionValue1)
issueManager.updateIssue(event.getUser(), issueToUpdate, EventDispatchOption.ISSUE_UPDATED, false)
}
else if(issueStatus.equals("Approved")||issueStatus.equals("Rejected")||issueStatus.equals("Obsoleted")){
issueToUpdate.setCustomFieldValue(customField, OptionValue2)
issueManager.updateIssue(event.getUser(), issueToUpdate, EventDispatchOption.ISSUE_UPDATED, false)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello,
Kindly have a look at my article how to update custom fields:
Basically you need to use the ComponentAccessor.getIssueManager().updateissue method
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
For instance We have one custom field with two options i.e "OPTION 1" and "OPTION 2"which should change according to status change.
For getting option ID we used
def OPTION_1Value = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {
it.toString() == 'OPTION 1'
}.getOptionId()
For issue to update
def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("<ISSUE_KEY>")
For updating custom field value
issue.setCustomFieldValue(custom_Field, OPTION_1Value)
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_ASSIGNED, false)
@Alexey MatveevWe tried your suggested method but still without any errors in logs, the custom field value is unchanged even though the status is changed.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You can add loggin to your script and make sure that this script is executed
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.