JIRA Behaviour plugin: handling sub-tasks

I was asked to show a warning message when editing an issue custom field, in case there are subtasks to the issue. Since I already had a validator script which was used for a similar purpose, I figured I’ll just copy the code into the relevant behavior entry and it would work. Clean and simple.

Boy, was I wrong.

As you may know, you need the issue object in order to work with its subtasks. In all Script Runner environments (validators/post functions/etc.) you have the readily available ‘issue’ variable. However it does not exist in the Behavior plugin environment. Since both were written by the same developer, I assumed it must be there somewhere, so tried all sorts of stuff to try and find it, but to no avail (why doesn’t it exist in the FieldBehaviours class?).

“Fine”, I said, “let’s get the issue key and obtain the object from it”. Not very elegant, but simple enough.

Wrong again. getFieldById(“issuekey”) returns null. I honestly have no idea why. Again I had to spend a lot of time searching for a reason, then gave up and started looking for a way to work around it. Eventually I figured out the correct way to get the issue object.

 

import com.atlassian.jira.ComponentManager
FormField field = getFieldByName("MyCustomField")
def issueid = getFieldById("id").value
def issue = ComponentManager.getInstance().getIssueManager().getIssueObject(Long.parseLong(issueid));
Collection subTasks = issue.getSubTaskObjects()
if (!subTasks.empty) {
 field.setHelpText("<div class=\"warningBox\">Attention: the issue has subtasks.</div>")
}
else {
 field.setHelpText("")
}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s