Modify script to set a sue date and time in a datetime field

I'm trying to set an issues DateTime field at issue creation time based on priority. I've started by using Mizan's script in this question:

However I want the datetime to be set more granularly. 1 hour for Critical, 2 Hours for high etc. I've modified the script sucessfully to use my own priorties but I can't figure out how I can set the datetime field to be set with the time as well as the date. Note I haven't even figured out how to call the custom field yet!

My version of the script is here:

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

// initializing the priority

def CRITICAL = 1;
def HIGH = 3;
def MEDIUM = 9;
def LOW = 30;

// calender which returns the date according to the priority defined

private GregorianCalendar getDate(double roll)
        Calendar cal = Calendar.getInstance();

        for (int x=0;x<roll;x++)

        return cal;

MutableIssue mutableIssue = (MutableIssue) issue;
def priority = mutableIssue.getPriority().getString("name");
def setDueDate = mutableIssue.getDueDate();
 //onlY set the dueDate if the date isn't already set (i.e. if it == null).

        GregorianCalendar cal;

                cal = getDate(CRITICAL);
   else if(priority.equals("High"))
                cal = getDate(HIGH);
   else if(priority.equals("Medium"))
                cal = getDate(MEDIUM);
        Timestamp dueDate = new Timestamp(cal.getTimeInMillis());

2 answers

Hey Graham,

Haven't tested this code but maybe you can get some ideas from it.

import com.atlassian.event.api.EventListener;
import com.atlassian.event.api.EventPublisher;
import com.atlassian.jira.bc.issue.IssueService;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.datetime.DateTimeFormatter;
import com.atlassian.jira.event.issue.IssueEvent;
import com.atlassian.jira.event.type.EventType;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueInputParameters;
import com.atlassian.jira.issue.priority.Priority;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import java.util.Calendar;

public class DueDateBasedOnPriority implements InitializingBean, DisposableBean {

    private static final Logger log = LoggerFactory.getLogger(DueDateBasedOnPriority.class);

    private final EventPublisher eventPublisher;
    private final IssueService issueService;
    private final JiraAuthenticationContext jiraAuthenticationContext;

     * Constructor.
     * @param eventPublisher injected {@code EventPublisher} implementation.
    public DueDateBasedOnPriority(EventPublisher eventPublisher, IssueService issueService, JiraAuthenticationContext jiraAuthenticationContext) {
        this.eventPublisher = eventPublisher;
        this.issueService = issueService;
        this.jiraAuthenticationContext = jiraAuthenticationContext;

     * Called when the plugin has been enabled.
     * @throws Exception
    public void afterPropertiesSet() throws Exception {
        // register ourselves with the EventPublisher

     * Called when the plugin is being disabled or removed.
     * @throws Exception
    public void destroy() throws Exception {
        // unregister ourselves with the EventPublisher

     * Receives any {@code IssueEvent}s sent by JIRA.
     * @param issueEvent the IssueEvent passed to us
    public void onIssueEvent(IssueEvent issueEvent) {
        Long eventTypeId = issueEvent.getEventTypeId();
        DateTimeFormatter dateTimeFormatter = ComponentAccessor.getComponent(DateTimeFormatter.class);
        Issue issue = issueEvent.getIssue();
        Priority priority = issue.getPriorityObject();
        // if it's an event we're interested in, log it
        if (eventTypeId.equals(EventType.ISSUE_CREATED_ID)) {
            IssueInputParameters issueInputParameters = issueService.newIssueInputParameters();
            IssueService.UpdateValidationResult updateValidationResult = issueService.validateUpdate(jiraAuthenticationContext.getUser().getDirectoryUser(), issue.getId(), issueInputParameters);
                IssueService.IssueResult updateResult = issueService.update(jiraAuthenticationContext.getUser().getDirectoryUser(), updateValidationResult);
                    //log your errors

    private DateTime getDueDateOnPriority(String priority){
        DateTime now = new DateTime();
            return now.withHourOfDay(Calendar.getInstance().get(Calendar.HOUR_OF_DAY) + 1);
            return now.withHourOfDay(Calendar.getInstance().get(Calendar.HOUR_OF_DAY) + 3);
            return now.withHourOfDay(Calendar.getInstance().get(Calendar.HOUR_OF_DAY) + 9);
            return now.withDayOfYear(Calendar.getInstance().get(Calendar.DAY_OF_YEAR) + 3);
            return now;

Hi Bhushan,

Thanks very much for this. It'll take me a while to work my way through it but I'll have a play and see how I get on!

Sorry about the bounty being added and cancelled. I'm trying to assign 5 karma but it keeps setting it to 30 which is a bit steep for me :/

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Feb 07, 2019 in Marketplace Apps

A Timeless Love Story

It started as any story starts, on a normal, rainy day.   Admin meets App, and her name was Klok2, and like any first relationship we were both trying to make it work but neither one knew what...

444 views 8 26
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you