Change the due date based on priority field

Stéphane Eursels May 12, 2020

Hi everyone,

I'm a newbie with groovy script and ScriptRunner.

I search a script in Post fonction to set the due date regarding the priority field value.

I have found some examples but they doesn't work.

The first example I've found is :

def dueDateField = issue.dueDate
def Sev = issue.priority

def dateToSet

if (Sev == "Critical") {
dateToSet = new Date() + 1
} else if (Sev == "Hight") {
dateToSet = new Date() + 5
} else if (Sev == "Medium") {
dateToSet = new Date() + 20
} else if (Sev == "Low") {
dateToSet = new Date() + 30
} else if (Sev == "Lowest") {
dateToSet = new Date() + 90
}

issue.setDueDate(dateToSet)

I have put it into the Custom ScriptRunner Post Fonction of creation transition.

I can save it without error and no failure when the script is running but the field Due date stay empty.

Do you have any idea ?

Thanks for your answer.

I work on Jira Software 8.3.4 with ScriptRunner 5.9.1-p5.

3 answers

1 accepted

0 votes
Answer accepted
Lasse Langhorn
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.
May 12, 2020

Hi @Stéphane Eursels

I have made some pseudo Groovy code. But important thing to look at is the last line in the code example where the actual issue update is happening.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue

def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

MutableIssue mutableIssue = issueManager.getIssueObject(issue?.id);

def priorityField = mutableIssue.priority
def dateToSet = null

if (priorityField == "Critical") {
dateToSet = new Date() + 1
} else if (priorityField == "Hight") {
dateToSet = new Date() + 5
} else if (priorityField == "Medium") {
dateToSet = new Date() + 20
} else if (priorityField == "Low") {
dateToSet = new Date() + 30
} else if (priorityField == "Lowest") {
dateToSet = new Date() + 90
}
// Set date
mutableIssue.setDueDate(dateToSet)

// Update issue
issueManager.updateIssue(user, mutableIssue, EventDispatchOption.ISSUE_UPDATED, false)

Hope this will help you get further.

Regards

Lasse Langhorn

Stéphane Eursels May 13, 2020

Fine it's work.

Thank's.

1 vote
Stéphane Eursels May 13, 2020

While looking for examples I came across several codes that allowed me to create the following code, which allows to shift the due date calculated if it falls on a weekend but nothing on the addition of working days and not calendar.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import java.sql.Timestamp;

def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

MutableIssue mutableIssue = issueManager.getIssueObject(issue?.id);

def priorityField = mutableIssue.priority
int dueInDays = 0

String priorityname = priorityField.getName()

if (priorityname == "Critical") {
    dueInDays = 1
} else if (priorityname == "High") {
    dueInDays = 5
} else if (priorityname == "Medium") {
    dueInDays = 20
} else if (priorityname == "Low") {
    dueInDays = 30
} else if (priorityname == "Lowest") {
    dueInDays = 0
}
if( dueInDays > 0 ) {
    Date today = new Date();
    Calendar MyDueDate = Calendar.getInstance();
    MyDueDate.add(Calendar.DATE,dueInDays)
    while ([Calendar.SATURDAY,  Calendar.SUNDAY].contains(MyDueDate.get(Calendar.DAY_OF_WEEK))) {
     MyDueDate.add(Calendar.DATE,1)
 }
 def dueTimestamp = new Timestamp(MyDueDate.getTimeInMillis())
 mutableIssue.setDueDate(dueTimestamp)

 issueManager.updateIssue(user, mutableIssue, EventDispatchOption.ISSUE_UPDATED, false)
}

 

Prakash May 19, 2021

@Stéphane Eursels  Thanks! This helped me to fulfil our user requirement! (y)

0 votes
Stéphane Eursels May 13, 2020

Is't a possibility to the number of days added are only working day?

In fact when the Priority is, for example "Medium", we want to add 20 working days of the current date to set the due date. Not just 20 days and to be sure that the due date is not a not working day.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events