It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

automatic subtask due date creation based on parent due date -14 days (Scriptrunner)

Basically I am trying to take the required parent due date, subtract 14 days from it and apply it to my subtask's due date field.

Currently it is setting the subtask due date as the created minus 14 days which is not what I want.

I want to set the parent to Apr/20/20 and then the subtask automatically, upon creation through a transition, sets its own due date to Apr/06/20.

 

The code I have right now in my subtask additional issue actions is:

import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import java.sql.Timestamp;

MutableIssue myIssue = issue;
Calendar dueDate = Calendar.getInstance();
myIssue.setDueDate(new Timestamp((new Date() - 14).time));

Any help would be much appreciated

1 answer

Hi


If I understand you correctly you need listener that fires when issue is updated
In listener you can
a) check if this is parent issue.
b) get parent issue due date
c) get all subtasks of this issue
d) update due date of each subtask.

Something like this:



Issue currentIssue = event.issue;

if(!(currentIssue.isSubTask())){
//this is parent issue
//get due date
Timestamp dueDateVal = currentIssue.getDueDate();

//then you can update due date of this issue
//review all subtasks and perform any operations with them

//e.g. update parent issue
MutableIssue issueToUpdate = (MutableIssue) event.issue
issueToUpdate.setDueDate(Timestamp '<your new dueDate value for parent issue>');

//get collection of subtasks
Collection<Issue> subTasksCol = currentIssue.getSubTaskObjects();

//iterate colleaction with subtasks and update duedate of each of them
for (Issue subTaskEl : subTasksCol) {
MutableIssue subTaskM = (MutableIssue) subTaskEl;
subTaskM.setDueDate(Timestamp '<your new dueDate value for subtasks>')
}

}

The parent due date will not be changed since it is required upon creation. So parent due date won't need to be updated. Upon transition of the parent to In Progress, the subtasks are all generated and need to have the parent due date set. I would prefer to use a post function for this, but I can try the listener and see if it throws any errors or gives me any issues.

Tried to test and it won't run. I set up a custom listener to fire when the the "issue updated" event occurs. Did not run once. Maybe I'm misunderstanding where to place this?

Also the Timestamp code gives me an error so I tweaked it to follow my code posted above.

import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import java.sql.Timestamp;

Issue currentIssue = event.issue;

if(!(currentIssue.isSubTask())){
//this is parent issue
//get due date
Timestamp dueDateVal = currentIssue.getDueDate();

//then you can update due date of this issue
//review all subtasks and perform any operations with them

//e.g. update parent issue
MutableIssue issueToUpdate = (MutableIssue) event.issue
issueToUpdate.setDueDate(new Timestamp((new Date() - 14).time));

//get collection of subtasks
Collection<Issue> subTasksCol = currentIssue.getSubTaskObjects();

//iterate colleaction with subtasks and update duedate of each of them
for (Issue subTaskEl : subTasksCol) {
MutableIssue subTaskM = (MutableIssue) subTaskEl;
subTaskM.setDueDate(new Timestamp((new Date() - 14).time));
}
}

1. you need to put your custom listener here

Manage apps -> Script listeners

custom_listener.png

2. Try to enable logging

import org.apache.log4j.Logger
import org.apache.log4j.Level
import org.apache.log4j.Category

//for log levels
Category log = Category.getInstance("com.onresolve.jira.groovy")
log.setLevel(org.apache.log4j.Level.INFO)
log.info("start logging ");

That is where I put my custom listener. Also I put the logging code in there, but I doubt its going to tell me anything since this hasn't actually run. Even though I have updated the test parent by transitioning it, the listener doesn't change anything.listener.PNG

Hmmm.... if this listener was not executed at all, looks like event was not fired or project scope is incorrect.

Could you create new custom listener with following parameters

a) change project scope to "All projects"

b) add new import row  in the top

import com.atlassian.jira.event.issue.AbstractIssueEventListener;

c) delete all code except import rows and log code

d) initiate "issue updated" event

e) see result - there is a "History" section

custom_listener.png

Okay so made the test listener and that ran, but didn't give any failures or anything I could really gleam from it. And then out of curiosity I updated the parent due date by one day and the other listener ran, but didn't change any of the due dates in the subtasks.

I would think the transition of the parent to In progress from open would count as an issue updated event, but apparently not.

This is all it gave me in their history sections:

listener logs.PNG

@Fyodorany other ideas?

Hi

Yuu wrote "other listener ran, but didn't change any of the due dates in the subtasks."

If listener was executed it means that event was fired.

You can enable logging (see my previous comment), then execute your listener and check all values you need.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Jira

The add-in you’ve been waiting for: Jira Cloud for Excel 🙌

Introducing Jira Cloud for Excel Here at the product integrations team at Atlassian, we are thrilled to announce the new Jira Cloud for Excel add-in! This add-in lets you export Jira data directly ...

952 views 8 25
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you