How display a message when sub-task is created?

Shah Baloch
Contributor
May 10, 2022

Hi Community,

Is it possible to display a message when a sub-task is created? Let me give some context. I configured task workflow so that once task status gets closed a sub-task gets created. We have a purchasing project, Initially, a task gets created by the user, they attach quotes and require documents to the task and after review from purchasing team, it goes to leadership for approval. Once it gets approved by leadership and the task gets assigned back to the reporter, once the user clicks the "place order" transition, the task resolution is set to Resolved, and a sub-task gets created automatically.

Most of the users won't notice the default popup that a sub-task is created so they keep attaching invoices etc to closed tasks and causing confusion. I'm looking to have a more visible popup or a message that users can see it. It'll be great to display the sub-task issue key as well.

Is it possible to display a message on the parent task when a sub-task is created?

Thank you for your help.

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
KC Wong
Contributor
May 10, 2022

I think you can make use of ScriptRunner's listener. Listen to issue created event, check if it is a subtask, if so, update the parent accordingly. 

e.g. You can use a label to display the message. 

import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.security.JiraAuthenticationContext
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue

log.warn("Issue: " + event.issue)
if (event.issue.isSubTask()) {
    log.warn("Is subtask, parent: " + event.issue.parentObject)
    MutableIssue parent = ComponentAccessor.getIssueManager().getIssueObject(event.issue.parentObject.id)
    // Here I simply update parent's description
parent.setDescription(
"Subtask created: " + event.issue.summary)
// Save it as current user
    JiraAuthenticationContext authContext = ComponentAccessor.getJiraAuthenticationContext()
    ApplicationUser user = authContext.getLoggedInUser()
    ComponentAccessor.getIssueManager().updateIssue(user, parent, EventDispatchOption.ISSUE_UPDATED, false)
}

 

Shah Baloch
Contributor
May 11, 2022

It is not displaying an alert. It replaces the parent issue description with "Subtask created: summary(subtask issue summary)". I thought of displaying an alert on the screen when a sub-task is created.

KC Wong
Contributor
May 11, 2022

That's just an example of how to listen to subtask created event.

You will have to find your preferred way of displaying a message. 

With Cloud it is not supported, see this thread: https://community.atlassian.com/t5/Jira-questions/JIRA-CLOUD-How-to-send-custom-Alerts-UserMessageUtil-for-Cloud/qaq-p/1756724 

If not Cloud, then that same thread mentioned UserMessageUtil class. You can find threads about it easily enough.

Failing both, you can use a field to display the message. 

Like Shah Baloch likes this
Shah Baloch
Contributor
May 12, 2022

I tried UserMessageUtil, and there are two issues I'm facing, first I have to refresh the page in order to see the message, and the second problem is it displays four pop-up messages instead of one. Any idea how can I fix these two issues?

import com.onresolve.scriptrunner.runner.util.UserMessageUtil

UserMessageUtil.success("This issue has been closed and a sub-task has been created for further process, please attach invovices to the sub-task. Sub-task can be found below.")

message_05122022.jpg

Thank you for your help.

KC Wong
Contributor
May 12, 2022

You are right, UserMessageUtil does not show the message unless the page is refreshed... but for your case, it should.

The create transition in the workflow has a post function to fire "Issue Created" event for listeners to catch, and afterwards the transition will refresh the page. 

Are you somehow creating subtasks without triggering the workflow transition?

 

As for the message repeating, I haven't been able to reproduce it. Try checking the logs on the listener to see if the listener got fired multiple times for the same issue?

Like Shah Baloch likes this
Shah Baloch
Contributor
May 13, 2022

You are right, I was creating sub-task manually. It works if an auto sub-task gets created. I updated the text color and size to make it more visible. One thing I'm unable to make work is the hyperlink for the issue key. Issue key displays but it's not clickable. Any idea?

Here is the script

 

import com.onresolve.scriptrunner.runner.util.UserMessageUtil
import com.atlassian.jira.event.issue.IssueEvent

if (issue.issueTypeObject.name == "Sub-task") {

UserMessageUtil.success("<h3 style='color:red;'><b>This Issue has been closed and a sub-task has been created for further process, please attach invovices to the sub-task. Sub-task can be found below.</b></h3>"
+ issue.key)
}

message_05132022.JPG

Thank you for your help

KC Wong
Contributor
May 15, 2022

You need to use the <a> tag to create a hyperlink: 

UserMessageUtil.success("<h3 style='color:red;'><b>This Issue has been closed and a sub-task has been created for further process, please attach invovices to the sub-task. Sub-task can be found below.</b></h3>"
+ "<a href='link to issue here'>" + issue.key + "</a>")
Like # people like this
Shah Baloch
Contributor
May 20, 2022

Thank you @KC Wong I was trying hyperlink but I was not including those plus (+) signs at beginning and end of the hyperlink so that's why it didn't work. I followed your steps and it works. I'm adding the final version below for those who don't have coding experience like me.

UserMessageUtil.success("<h3 style='color:red;'><b>This Issue has been closed and a sub-task has been created for further process, please attach invovices to the sub-task. Sub-task can be found below.</b></h3>"
+ "<a href='https://myjira.com/browes/$issue'>" + issue.key + "</a>")
 
One last question, is there a way to include the parent issue key in the popup? Like I add " This issue NA-60 (parent issue key) has been closed". Is that possible?

Thank you so much for your help!

KC Wong
Contributor
May 22, 2022

issue.parentObject.key

Like Shah Baloch likes this
Shah Baloch
Contributor
May 23, 2022

Thank you so much for your help.

KC Wong
Contributor
May 23, 2022

I just started Jira development this month. So it's good learning experience for me too. 

TAGS
AUG Leaders

Atlassian Community Events