I am trying to compare a value from a multiselect customfield. i did the following code:
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.user.util.UserManager;
import com.atlassian.jira.issue.MutableIssue
import com.onresolve.scriptrunner.runner.util.UserMessageUtil
def issueManager = ComponentAccessor.getIssueManager();
def userManager = ComponentAccessor.getUserManager();
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def cField = customFieldManager.getCustomFieldObject("customfield_12032");
def cFieldValue = issue.getCustomFieldValue(cField);
if (cFieldValue.toString() == "Business Trips")
{
UserMessageUtil.error('Please select project.')
}
But the code only works when I put cFieldValue.toString() == "None". I can't seem to figure out why it doesn't work for the other options.
Hi
Welcome to the Atlassian Community.
I don't think you need to build new screens etc.
You can use behaviour to manipulate the list of issue types offered in the drop down.
The trick is that you need to get information about the parent issue (to know which issue type to show or hide).
And you'll want to make sure that the behaviour only fires when a sub-task is being created (not the parent issues).
Try something like this (in the initializer of a behaviour for your project either mapped to all issue types or just the 2 sub-tasks)
import com.atlassian.jira.component.ComponentAccessor
if(issueContext.issueType.isSubTask()){ //this may not be needed if the behaviour mapping is only for the sub-tasks
def issueTypeField = getFieldById('issuetype')
def parentIssueIdFld = getFieldById('parentIssueId')
def parentIssue = ComponentAccessor.issueManager.getIssueObject(parentIssueIdFld.value as Long)
def allIssueTypes = ComponentAccessor.constantsManager.allIssueTypeObjects
def subtaskName
if(parentIssue.issueType.name == 'Bug'){
subtaskName = 'Defect'
} else {
subtaskName = 'Sub-task'
}
issueTypeField.setFieldOptions(allIssueTypes.findAll{it.name == subtaskName})
issueTypeField.setFormValue(allIssueTypes.find{it.name == subtaskName}.id)
}
Thank you this code actually worked. It tested it further and added a new subtask by name ' Test Sub-task' , I went ahead and created a create subtask under bug issue type and on the create sub task screen , under the Issue type drop down , I was able to select the Newly created 'Test Sub-task' from the Issue type drop down. I believe any time a new subtask is added , The new subtask will be displayed under the issue type drop down. How do I handle this issue in future any time a new subtask type is added.
I would like Bug issue type(Parent issue) to have only 'Defect' Sub task associated with it all the time even if a new Subtask like ' Test Sub- task ' created.
I have hidden behavior to 'Sub-task' subtask in the Script runner UI Hide element behavior.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
If you want defect for Bug and all-but defect for non-bug, then the flow of the script could look a little like this:
def allowedSubtasks = []
if(parentIssue.issueType.name == 'Bug'){
allowedSubtasks = allIssueTypes.findAll{it.name == 'Defect'}
//since there is only 1 available subtaks, might as well select it
issueTypeField.setFormValue(allowedSubtasks[0].id)
} else {
allowedSubtasks = allIssueTypes.findAll{it.name != 'Defect'}
}
issueTypeField.setFieldOptions(allowedSubtasks)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Garden16,
How to add more subtasks to the code? I try to add one more subtask with another name and I can't.
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.