Dynamically set Due Date depending on Day of Week.

Michael Schultz April 29, 2019

Hello. Can someone do a quick look over on this listener I built?

Purpose: Automatically set the due date when an issue is created. Due Date set is based on which day of the week the ticket was created.

Example: if it was created on a Monday AND does not contain a specific label, set Due Date to Now + 5 days. If the ticket is created any other day of the week AND does not contain a specific label, set Due Date to Now + 6 days.

Here is my listener:

import java.sql.Timestamp;
import java.util.Date;
import java.util.*;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueInputParametersImpl;
import org.apache.log4j.Category;
import com.atlassian.jira.issue.MutableIssue;
import java.time.LocalDate;
import java.text.SimpleDateFormat;


def Category log = Category.getInstance("com.onresolve.jira.groovy")
log.setLevel(org.apache.log4j.Level.INFO)

Issue issue = event.issue;
MutableIssue myIssue = issue as MutableIssue;
Set issueLabels = issue.getLabels();


boolean containsLabel = false;
Date date = new Date();
long time = date.getTime();
log.info "time = " + time.toString()

issueLabels.each { label ->
if (label.toString().contains("exampleLabel")){
containsLabel = true;
log.info 'containsLabel = ' + containsLabel.toString()
}
}

log.info "Existing Due Date = " + myIssue.getDueDate().toString()


Calendar cal = Calendar.getInstance();
String createdDate = issue.getCreated().toLocalDateTime().format(java.time.format.DateTimeFormatter.ofPattern("yyyy/MM/dd HH:MM"))
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:MM", java.util.Locale.ENGLISH);
Date myDate = sdf.parse(createdDate.toString());
sdf.applyPattern("EEEE");
def String sMyDate = sdf.format(myDate);
log.info 'Created Date = ' + sMyDate.toString();


if ((sMyDate.toString().contains('Monday') || sMyDate.toString().contains('Sunday')) && issue.issueType.name == "Task" && containsLabel == false && myIssue.getDueDate().toString() == 'null') {
myIssue.setDueDate(new Timestamp (cal.getTimeInMillis() + 5 * 1000 * 24 * 60 * 60));
log.info 'dueDate = ' + myIssue.getDueDate().toString()
}
else if ((!sMyDate.toString().contains('Monday') || !sMyDate.toString().contains('Sunday')) && issue.issueType.name == "Task" && containsLabel == false && myIssue.getDueDate().toString() == 'null') {
myIssue.setDueDate(new Timestamp (cal.getTimeInMillis() + 6 * 1000 * 24 * 60 * 60));
log.info 'dueDate = ' + myIssue.getDueDate().toString()
}

 

I am able to see the 'dueDate = value' portion in the logs and it is not null, meaning the dueDate variable has been set. However, the issue's Due Date never actually updates. So something must be wrong with:

 

myIssue.setDueDate( new Timestamp (cal.getTimeInMillis() + 5 * 1000 * 24 * 60 * 60 * 60))

 

1 answer

1 vote
Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 30, 2019

Hi @Michael Schultz ,

Kindly try this snippet to set due date according to current day : 

Calendar c1 = GregorianCalendar.getInstance();
c1.setTime(new Date())
if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY && !containsLabel){
c1.add(Calendar.DAY_OF_MONTH, 5);
}
else if (c1.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY && !containsLabel){
c1.add(Calendar.DAY_OF_MONTH, 6);
}
issue.setDueDate(c1.getTime().toTimestamp())

Antoine

Michael Schultz April 30, 2019

@Antoine Berry Thanks for this solution. This works as did my solution before. However, I still can't get it to work in one project. It works in my test project. I've checked permissions, Screens, Field Config and I don't see any difference. 

Any idea why I just wouldn't be able to change a specific project's Due Date when it works in another? Schedule Issues permission is set correctly by the way.

Antoine Berry
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 2, 2019

Hi @Michael Schultz , 

Maybe check the workflow. Especially check the order of the postfunctions in the "Create Issue" transition, make sure your script is ran after the "Creates the issue originally.".

Antoine

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events