Snippets

Adaptavist IncrementDueDateByANumberOfWeekDaysPostFunctionJiraCloud

Created by Kristian Walker last modified
/*
 * "This script post function script provides an example of how to increment the due date by a number of weekdays specified. 
 * All right, title and interest in this code snippet shall remain the exclusive intellectual property of Adaptavist Group Ltd and its affiliates. Customers with a valid ScriptRunner 
 * license shall be granted a  non-exclusive, non-transferable, freely revocable right to use this code snippet only within their own instance of Atlassian products. This licensing notice cannot be removed
 * or amended and must be included in any circumstances where the code snippet is shared by You or a third party." 
 */
 
// Get the issue key
def issueKey = issue.key

// Get today's date to set as the due date
def today = new Date()

// Specify the number of weekdays to add onto todays dat
def numberOfWeekdays = <AddNumberOfDaysHere>

// Add the specified number of weekdays onto the due date
if (numberOfWeekdays) {
    
    // define a date object to update
    Date result = today;
    
    // Get the time value for todays date
    Date newDate = new Date(result.getTime())
    
    // Specify a loop counter
    int i = 0;
    
    // Loop over for the number of weekdays to add
    while (i < numberOfWeekdays) {
        // increment the date by 1 day
        result = result + 1
        newDate = new Date(result.getTime())
        // Check if the date is a weekend and only increment the loop counter if it is not
        if (newDate[Calendar.DAY_OF_WEEK] == Calendar.SATURDAY || newDate[Calendar.DAY_OF_WEEK] == Calendar.SUNDAY) {
            numberOfWeekdays++;
        }
        i++;
    }
    
    // Update the issue to set the due date to its new incremented value
    def updateDueDate = put("/rest/api/2/issue/${issueKey}")
            .header('Content-Type', 'application/json')
            .body([
            fields: [
                    // Set the due date to today's date in the format which Jira expects the date to be in
                    duedate: newDate.format('yyyy-MM-dd') as String
            ]
    ])
            .asString()
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.