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

erin October 29, 2019

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

0 votes
fjodors
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.
October 30, 2019

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>')
}

}
erin October 30, 2019

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.

erin October 30, 2019

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));
}
}
fjodors
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.
October 30, 2019

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 ");
erin October 30, 2019

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

fjodors
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.
October 30, 2019

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

erin October 30, 2019

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

erin November 1, 2019

@fjodorsany other ideas?

fjodors
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.
November 3, 2019

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.

fjodors
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.
November 4, 2019
erin November 18, 2019

Maybe my original explanation is confusing. I want to take the parent's dueDate (i.e. Apr/20/20) apply it to the subtasks, but subtract a certain amount of days, say 14, so the subtasks due date would be (Apr/06/20).

I think a listener would work in this case but your suggestions were to use other date picker type fields and base it off of that. If I need to make another date picker field for this to work I will but I thought maybe something like this may work as well? https://community.atlassian.com/t5/Jira-questions/Custom-update-listener-to-set-subtask-s-fix-version/qaq-p/591808 So instead of fixVersions I would use dueDate

@Nic Brough -Adaptavist-i know you in the past suggested how to set a duedate based on creation date minus a certain amount of days.

Like LCD likes this

Suggest an answer

Log in or Sign up to answer