Need help to set DueDate automatically filled in as 5 days after creation of issue

Vijaya Kumara November 18, 2019

Dear All,

I am quite new to Jira / Confluence and I am not from development background.

I need to set an Automatic date for Duedate for issues = 5 days after creation of Issue should be DueDate, Should exclude Saturdays and Sundays.

I used a expression / Code from blog as below, but got error messages. To be honest I don't know anything about programming.

Used expression

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

Error message

Unable to resolve class Timestamp @Line 1, column19

I am using Jira v7.13.3

 

Could you please help and guide

2 answers

1 accepted

1 vote
Answer accepted
Leonard Chew
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 19, 2019

Hi there

Do it with a Scriptrunner Postfunction.

  1. Create a scriptrunner postfunction on your create transition, type: custom
  2. Move the Postfunction between the creation step and the re-index step
  3. Add this code
import org.apache.log4j.Level
log.setLevel(Level.DEBUG)

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

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueManager = ComponentAccessor.getIssueManager()
def creation = issue.getCreated()
def daysToShift = 5

def newDueDate = creation + daysToShift
def dayOfWeek = newDueDate.getDay()

switch (dayOfWeek) {
case 0 : newDueDate = newDueDate + 1 // sunday
log.debug('Adding 1 day to Sunday')
break
case 6 : newDueDate = newDueDate + 2 // saturday
log.debug('Adding 2 days to Saturday')
break
}

issue.setDueDate(newDueDate)
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

log.debug(issue.key+': Due Date has been set to ' + newDueDate)

 

If it works, you can remove the debug level in production by changing level.DEBUG to level.WARN

Vijaya Kumara November 19, 2019

@Leonard Chew - Thank you for all the information, I just a beginner - Could you please send some screen shots on steps I need to follow to get this Implemented.

I tried below option but didn't know how to proceed to next step.

I created a transition in workflow as custom

 

Jira_Custome.jpgI also got below error messages in console

console_errors.jpg

Leonard Chew
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 19, 2019

Hmm, if you lack knowledge of jira-administration basics, it might be wise to persuade your manager to give you an administration course, before you work with scripts.

That said, here are the answer to your questions:

1) General: The script will not work if you paste it in to the script console, as the console does not know on which issue it should run on.
In a postfunction-script, the issue is automatically set to the issue currently being transitionend (created in our case).

2) Do NOT create a new transition. This postfunction should run on the initial "Create"-Transition, which is the line from the circle to the first status. The script automatically sets the due date when an issue is created through this workflow.

Here are some screenshots pasted together to get it to work:

 

scriptrunner-postfunction-autoDueDate.png

Like Vijaya Kumara likes this
Vijaya Kumara November 19, 2019

@Leonard Chew  - Thanks a lot for your support, I was able to complete the steps. But I don't see Due Date getting filled in Automatically.

Is there any additional steps I need to complete.

console_postfunction.jpg

Leonard Chew
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 19, 2019

@Vijaya Kumara  Create an Issue (one that is created by this workflow). The script will be executed and you will see the logs (of the last 15 executions) in the screen you pasted above.

Vijaya Kumara November 19, 2019

@Leonard Chew - Yes I did create couple of Issues which are using the Workflow where Script was saved using Steps explained by you.

Vijaya Kumara November 19, 2019

@Leonard Chew - It works, thank you for all suport

Like Leonard Chew likes this
Vijaya Kumara November 19, 2019

@Leonard Chew  - I have one more request, Now Due Date is created Automatically as 5 days after Issue creation. But this overwrites even If I fill a Due date while creation of Issue, can we set this option only to Issues which are missing Due Date information?.

Leonard Chew
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 19, 2019
if (issue.getDueDate()) {return}
Vijaya Kumara November 20, 2019

@Leonard Chew - In your program where I should use this expression / Code line

Leonard Chew
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 20, 2019

The code snippet exits the script if due date is set.

If you administer scripts, you will need to be able to read it at least a little bit, so try to figure out where it belongs. Try it out in different places on your testing system. You can use 

log.debug('My Information')

 to help get information.

Like Vijaya Kumara likes this
1 vote
Ravi Sagar _Sparxsys_
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 18, 2019

Hi @Vijaya Kumara 

Where exactly did you try the expression that you mentioned above?

You would need an app/add-on like ScriptRunner for doing this.

Ravi

Vijaya Kumara November 18, 2019

Hi @Ravi Sagar _Sparxsys_ Yes I have Script Runner add on 

Suggest an answer

Log in or Sign up to answer