Restrict sub-task options based on parent issue type

- May 19, 2021

Is there a way with scriptrunner to restrict the sub-tasks that you can create depending on the parent issue that you're creating it in?

I want to limit the sub-task options when creating under a standard issue type called "Comms" to just "Plan", "Weekly Download", "Slack", "Email", "Weekly Wrap", "The Street", "Edit",  and "Write".

Referencing SRJIRA-1010, I thought this initialiser script would do the trick:

 

import static com.atlassian.jira.issue.IssueFieldConstants.ISSUE_TYPE
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor

Issue parentIssue = underlyingIssue.parentObject

def constantsManager = ComponentAccessor.getConstantsManager()
def commsIssueType = constantsManager.getAllIssueTypeObjects().find { it.name == "Comms" }

if(parentIssue.getIssueType().equals(commsIssueType)){
def subtaskSet = constantsManager.getAllIssueTypeObjects().findAll
{ it.name in ["Plan", "Weekly Download", "Slack", "Email", "Weekly Wrap", "The Street", "Edit", "Write"] }
.collectEntries{
[(it.id)]}

getFieldById("issuetype").setFieldOptions(subtaskSet)
}

 

But unfortunately I still see other sub-task issue types still available.

3 answers

1 vote
Max Lim _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 23, 2021

Hi @Ian Balas 

You can refer to this documentation on how to retrieve the parentIssue properly.

I can confirm it does work. I hope this helps!

Max

1 vote
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 23, 2021

Hi @Ian Balas

At the moment, this is not doable using the Behaviour.

The alternative would be to use javascript, as discussed in this community post

Another alternative suggested in this community post is to use ScriptRunner's validator.

I hope this helps to answer your question. :)

Thank you and Kind Regards,

Ram

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 26, 2021

Hi @Ian Balas

If this answers your question, could you please accept the answer provided?

Thank you and Kind Regards,
Ram

- May 27, 2021

Hi @Ram Kumar Aravindakshan _Adaptavist_ 

The validator sort of helps. I'm able to restrict the user from creating unwanted subtasks for the parent issue type of concern (Comms). But it would be better if I could hide the unwanted subtasks as well to avoid confusion. 

The js solution I have yet to implement. This would be set as a validator as well?

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 27, 2021

Hi @Ian Balas

To add the Javascript, you try using ScriptRunner's Web Resource, or the alternative would be Jira's Announcement Banner.

I hope this helps to answer your question. :)

Thank you and Kind Regards,

Ram

0 votes
Becky July 24, 2023

Hi @Ian Balas

I am trying to achieve the same thing as you and the following code using ScriptRunner works for me. Hope this can help others with the same needs!

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import static com.atlassian.jira.issue.IssueFieldConstants.ISSUE_TYPE

@BaseScript FieldBehaviours fieldBehaviours

def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
def issueManager = ComponentAccessor.getIssueManager()
def allIssueTypes = ComponentAccessor.constantsManager.allIssueTypeObjects
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def parent = getFieldById("parentIssueId")
Long parentIssueId = parent.getFormValue() as Long
def parentIssue = issueManager.getIssueObject(parentIssueId)

def issueTypeField = getFieldById(ISSUE_TYPE)
def remoteUsersRoles = projectRoleManager.getProjectRoles(user, issueContext.projectObject)*.name
def availableIssueTypes = []
if (parentIssue.issueType.name in ["Story", "Bug"]) {
    availableIssueTypes.addAll(allIssueTypes.findAll { it.name in [ 'QD'] })
issueTypeField.setFieldOptions(availableIssueTypes)
Raphi Levine February 20, 2024

HI @Ian Balas ,

This looks interesting to me and I would like to implement it.

I am not familiar with Script Runner.  Would this be created as a Behavior in Script Runner?

Becky February 20, 2024

Hi @Raphi LevineYes, this is implemented using ScriptRunner behavior. I simply pasted the code to the initializer section.

Like Raphi Levine likes this
Raphi Levine February 21, 2024

I have Jira Cloud and not Server.

When I paste the above in, it shows Invalid Imports with this message.

"It looks like you're trying to import some Java classes intended to work with Jira Server. Unfortunately these won't work due to the differences between Jira Cloud and Jira Server.

ScriptRunner for Jira Cloud is designed to be used with the Jira Cloud REST APIs."

Any idea how I can accomplish this?

Becky February 22, 2024

Hi @Raphi Levine

I should have mentioned my organization used Jira Data Center but not Jira Cloud. I am afraid I don't know much about how the ScriptRunner works on the Jira Cloud side : (

I think you may want to look into the difference in ScriptRunner "Behavior" specifically: https://docs.adaptavist.com/sr4jc/latest/differences-between-scriptrunner-for-jira-server-and-jira-cloud/feature-parity#:~:text=alternatives%20are%20available.-,Behaviours,-Server/DC%20Feature

Suggest an answer

Log in or Sign up to answer