This post is the continuation of:

To evolve the script, to put for example Working days instead of natural days, when setting a due date can be somewhat difficult using Scriptrunner for Jira Cloud, the reason is that we cannot use “while” loops, we must optimize our code As much as possible, in the following example, we will use MATHS to calculate the duedate in Working days.

In this example we set DUE DATE = TODAY + 1 ( BUT, if its Sunday or Saturday it becomes MONDAY)… but ¿What happen if we want more days?

In reality we use maths and a formula to calculate the Business days, in this case is:

DUE DATE = TODAY + (Businessdays + ( 2*(Businessdays/5)))

//Here the imports to use classes for Time and Dates
import java.text.SimpleDateFormat
import java.sql.Timestamp
import java.text.DateFormat
import java.util.Date

//we set in the new variable duedate the value of the Duedate of the issue
//Date duedate = issue.fields.duedate
int bussdays = 1

//To get the actual DATE
Date tomorrow = new Date() + (bussdays + (2*(bussdays/5))).toInteger()

//Calendar cacheCalendar = Calendar.instance

def weekday = new Date().parse('yyyy-MM-dd', tomorrow.format('yyyy-MM-dd').toString())[Calendar.DAY_OF_WEEK];
if (( weekday == Calendar.SUNDAY)) {
    tomorrow = tomorrow + 1 //1 day
}
if ((weekday == Calendar.SATURDAY)) {
    tomorrow = tomorrow + 2 //2 day
}

def tomorrow_new = new Date().parse('yyyy-MM-dd', tomorrow.format('yyyy-MM-dd').toString()).format('yyyy-MM-dd');

//We are setting the new due date
issueInput.fields.duedate = tomorrow_new

but imagine you need to update other field different than the standard Due date, image is a Date-Time custom field of your Jira Cloud, then we need to change the last part of the script to:

//Here the imports to use classes for Time and Dates
import java.text.SimpleDateFormat
import java.sql.Timestamp
import java.text.DateFormat
import java.util.Date

//we set in the new variable duedate the value of the Duedate of the issue
//Date duedate = issue.fields.duedate
int bussdays = 1

//To get the actual DATE
Date tomorrow = new Date() + (bussdays + (2*(bussdays/5))).toInteger()

//Calendar cacheCalendar = Calendar.instance

def weekday = new Date().parse('yyyy-MM-dd', tomorrow.format('yyyy-MM-dd').toString())[Calendar.DAY_OF_WEEK];
if (( weekday == Calendar.SUNDAY)) {
    tomorrow = tomorrow + 1 //1 day
}
if ((weekday == Calendar.SATURDAY)) {
    tomorrow = tomorrow + 2 //2 day
}

def tomorrow_new = new Date().parse('yyyy-MM-dd', tomorrow.format('yyyy-MM-dd').toString()).format('yyyy-MM-dd');

//We are setting the new due date
def post_result = put("/rest/api/2/issue/${issue.key}") 
 .header("Content-Type", "application/json")
 .body([
 fields:[
 customfield_10129: tomorrow_new
 ]
 ])
 .asString()

if (post_result.status == 204) { 
 return 'Success'
} else {
 return "${post_result.status}: ${post_result.body}"
} 

By MrAddon

.

Posted by:.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s