Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Trying to add a auto due using a post-funtion

Warren McInnes
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.
February 10, 2013

Hi,

I'm attempting to automatically add a "Due Date" to imcoming emails into jira via an external email client such as Outlook.

So i've used:

Copy Value From Field to Field: Copies the value(s) of one field to another field. Can copy the value of the field before it was changed on the transition's screen.'

Add i've added it to the "Create" state in the workflow:

Add a new post function to the unconditional result of the transition.

Creates the issue originally.
— THEN
The Field Due Date will take the value from Created .
Edit | Move Up | Move Down | Delete
— THEN
Fire a Issue Created event that can be processed by the listeners.
Edit

Which seems to work, However When I Create the issue via the "Create issue" button internally this still overides any due date selected and uses the create date.

How can i do it so that if for instance if due date is field is empty to then use "Copy Value From Field to Field" otherwise if due date is not empty to continue?

We have an problem with emails/issues that come into jira without a due date and then it does not get worked on. so we need to enforce a due date if not added?

any suggestions?

*PS. Hope it makes sense?

5 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
Henning Tietgens
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.
February 10, 2013

You can use the Script Runner plugin and a scripted post function, which tests if the due date is set and if not updates it with the created date. Maybe a good start is the example postfunction here.

Henning

Warren McInnes
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.
February 10, 2013

Hi, I was thinking that route, but i'm not too sure how?

These are my only options when writing a post-function:

Built-in Scripts

Adds a comment to all blocked issues when this issue is transitioned.
Useful for alerting participants of other issues that a blocker is resolved, etc.
This function should be put on the Resolve transition (or similar).

Send a custom email
Send an email based on the provided template if conditions are met

Fast-track transition an issue
If the condition is met, automatically transition this issue to another status

Clones an issue and links.
Clones this issue to another issue, optioninally in another project, and optionally a different issue type.

Transition parent when all subtasks are resolved
This will do the given action on the parent when all sub-tasks are resolved

Create a sub-task.
Create a sub-task. Will optionally reopen a matching sub-task.

Fires an event when condition is true
Fires an event that can be picked up by a notification scheme, in order to send mail only under certain conditions, eg Priority is Blocker

Adds the current user as a watcher
Adds the user performing the action as a watcher, if condition applies

or
Script file path:

Would is have to write a separate file an use: Script file path?

What format would i save it?

Could you elborate for me?

Warren McInnes
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.
February 10, 2013

Do i need a special program to write .groovy files?

Thanks for your help

Henning Tietgens
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.
February 10, 2013

The script is something like this (not tested by me, I'm not sure if you get the due date in the issue, maybe it depends on the position of the postfunction in the postfunction list)

import com.atlassian.jira.issue.MutableIssue

MutableIssue mIssue = issue

if (!mIssue.getDueDate()) {
    mIssue.setDueDate(mIssue.created)
}

Henning

Henning Tietgens
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.
February 10, 2013

Save it as SetDueDate.groovy on the server and provide the path while adding it to the workflow.

Henning Tietgens
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.
February 10, 2013

A text editor is fine.

Warren McInnes
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.
February 11, 2013

I ran the above script in the script runner's condition tester, it failed? I'm going to need some help with groovy scripts as i'm not too fluent in groovy?

how is "Mutable" relevant?

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 11, 2013

An issue object represents an issue. A mutable-issue object represents an issue that is in the process of changing - you've got access to the current issue data and what the users may have changed on it. So if you're changing an issue, you work with a mutable issue.

Henning Tietgens
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.
February 11, 2013

What kind of error did you get in the condition tester? I think within the condition tester you don't have a mutable issue as issue and the changes will not be saved. Here you could only test conditions for other scripts, which do tests on current issue data. If you use the script as a scripted postfunction, the mutable issue is saved by the system after the script changed it.

Henning

Warren McInnes
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.
February 11, 2013

Strangely no error just The result evaluated to false

Henning Tietgens
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.
February 11, 2013

Yes, that's the value of the last operation of the script. If you wan't to try it within the condition tester you have to modify the script to this

import com.atlassian.jira.issue.MutableIssue

MutableIssue mIssue = issue

if (!mIssue.getDueDate()) {
    mIssue.setDueDate(mIssue.created)
}
mIssue.store()

This saves this issue object, but you don't need this for the post function.

1 vote
Warren McInnes
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.
February 9, 2014

Hi guys, as per this issue: https://answers.atlassian.com/questions/195955/automatic-due-date-by-issue-type-when-creating-ticket?page=1#260376

I have managed to get Automated due date, How ever is there any way to exclude weekends? Or for instance if the due date falls on a weekend that it gets changed to the very next monday?

Thanks in advance

Henning Tietgens
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.
February 9, 2014

The dates you get in the scripts are Java Timestamps. You could do whatever you need with them before you write them to the issue. For checking Java Timestamps for weekends etc. a Google search should help. A good starting point should be Calendar.

Warren McInnes
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.
February 10, 2014

Hi Henning, I'm not sure how well versed you are on Java Timestamps, but I've come up with this and it doesn't seem to be working?

import org.apache.log4j.Category
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.CustomFieldType
import com.atlassian.jira.issue.fields.CustomField
import java.sql.Timestamp; 
 
Timestamp mydueDate;
int days = 5;
int dayOfWeek;
MutableIssue myIssue = issue;
Calendar cal = Calendar.getInstance();

do {
    // add due date one day into the future
    mydueDate = new Timestamp(cal.getTimeInMillis() + days*1000*24*60*60);

    cal.setTimeInMillis(mydueDate.getTime());
    
    dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
    
    //System.out.println(String.format("Day of week is %d / days: %d", dayOfWeek, days));
    
    days = days + 1;
    
} while ((dayOfWeek == Calendar.SATURDAY) || (dayOfWeek == Calendar.SUNDAY));

  
myIssue.setDueDate(mydueDate);

log.warn ("script setduedate is running...and mydueDate is:" + mydueDate)

Any suggestions?

Henning Tietgens
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.
February 11, 2014

Mmmh. Do {} while doesn't exist in Groovy... Didn't you get an error message in the log?

Try this

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

MutableIssue myIssue = issue;
Calendar duedate = Calendar.getInstance()

duedate.add(Calendar.DATE,1)
while ([Calendar.SATURDAY, Calendar.SUNDAY].contains(duedate.get(Calendar.DAY_OF_WEEK))) {
    duedate.add(Calendar.DATE,1)
}
def dueTimestamp = new Timestamp(duedate.getTimeInMillis())
myIssue.setDueDate(dueTimestamp)
log.warn ("script setduedate is running...and dueTimestamp is:" + dueTimestamp)

I didn't test it, but I think it's a good start :-)

Warren McInnes
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.
February 11, 2014

Hi Henning,

I did see that only after I posted. However I did manage to come right with this code... So far seems to be working:

import org.apache.log4j.Category
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.CustomFieldType
import com.atlassian.jira.issue.fields.CustomField
import java.sql.Timestamp; 
 
  	int days = 8;
        int dayOfWeek = 0;
        Date today = new Date();
	MutableIssue myIssue = issue

        Calendar dueDate = Calendar.getInstance();

         while ((dayOfWeek == 0) || (dayOfWeek == Calendar.SATURDAY) || (dayOfWeek == Calendar.SUNDAY)) {
            dueDate.setTimeInMillis(today.getTime() + days*1000*24*60*60);
            dayOfWeek = dueDate.get(Calendar.DAY_OF_WEEK);
            days = days + 1;
         }
        
        System.out.println("script setduedate is running...and mydueDate is:" + dueDate.getTime());

	myIssue.setDueDate(new Timestamp(dueDate.getTime().getTime()));

Thanks for all your halp and knowledge by the way

0 votes
Warren McInnes
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.
February 9, 2014

Thanks Henning, I will have a look into this.

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 10, 2013

You can't. The functions you've found are flat and unconditional - they do what they say on the tin and "copy value from field to field" without any form of checking of pre-existing data.

You'll need to write a function which duplicates these, but includes some extra code to say "already got a date, so I won't override it"

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 10, 2013

Write your own plugin that provides a post-function that includes the conditional code you need.

Henning has pointed at the script runner (that saves you having to write an entire plugin)

Warren McInnes
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.
February 10, 2013

Can you elaborate on

You'll need to write a function which duplicates these, but includes some extra code to say "already got a date, so I won't override it"

Write a function, how?

Warren McInnes
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.
February 10, 2013

So Hennings approach is good?

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 10, 2013

Yes, it does what we said - enables a post function that creates the "if" you need.

Warren McInnes
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.
February 11, 2013

Thanks for your help

Joe Cavanaugh November 21, 2018

Using Henning's code didn't quite work for me. It was overwriting customer set due dates. I made a small change, added an if statement, and now it's working for me. I also changed mine to set the due date to two days after the issue is created.

 

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

MutableIssue myIssue = issue;
Calendar duedate = Calendar.getInstance()

if (!myIssue.getDueDate()) {
duedate.add(Calendar.DATE,2)
while ([Calendar.SATURDAY, Calendar.SUNDAY].contains(duedate.get(Calendar.DAY_OF_WEEK))) {
    duedate.add(Calendar.DATE,1)
}
def dueTimestamp = new Timestamp(duedate.getTimeInMillis())
myIssue.setDueDate(dueTimestamp)
}

 

Casey Maynard May 14, 2019

I often see solutions that use 'code', but I never know where I should put this information.

I want to be able to map my Fix Version Release date into my Due Date for a card.

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events