Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Script-Runner Working days between two custom date fields (Datefields only)

Amar April 5, 2019

Hi Folks,

 

I have below script to calculate Working days between two custom date fields which are only date fields (NO time stamp).

Please help me to understand "where i am wrong in below script"

 

=================================

customfield_16160 = 12/Mar/19

customfield_16281 =20/Mar/19

 

Answer must be = 6 (working days) 

 

Currently it gives 0 or Null

 

===============================

 

Script :

import com.atlassian.jira.component.ComponentAccessor
import java.sql.Timestamp
import java.time.DayOfWeek
import java.text.SimpleDateFormat;


def Plannedstartdate=ComponentAccessor.customFieldManager.getCustomFieldObject("customfield_16160")
def dateB=ComponentAccessor.customFieldManager.getCustomFieldObject("customfield_16281")


if (!issue.getCustomFieldValue(Plannedstartdate) || !issue.getCustomFieldValue(dateB)) {
return null
}

def startdate = (issue.getCustomFieldValue(Plannedstartdate)as Timestamp).toLocalDateTime().toLocalDate()
def enddate = (issue.getCustomFieldValue(dateB) as Timestamp).toLocalDateTime().toLocalDate()

if (!enddate.isBefore(startdate)) {
return null
}

def weekend = EnumSet.of(DayOfWeek.SATURDAY, DayOfWeek.SUNDAY)
def workingDays = 0

while (enddate.isBefore(startdate)) {
if (!(enddate.dayOfWeek in weekend)) {
workingDays += 1
}

enddate = enddate.plusDays(1)
}

workingDays

 

 

Regards,

Amar

 

 

3 answers

1 accepted

1 vote
Answer accepted
Tom _Automation Consultants_
Atlassian Partner
April 5, 2019
import com.atlassian.jira.component.ComponentAccessor
import java.sql.Timestamp
import java.time.DayOfWeek
import java.text.SimpleDateFormat;

def plannedStartDate=ComponentAccessor.customFieldManager.getCustomFieldObject("customfield_16160")
def otherDate = ComponentAccessor.customFieldManager.getCustomFieldObject("customfield_16281")


if (!issue.getCustomFieldValue(plannedStartDate) || !issue.getCustomFieldValue(otherDate)) {
return null
}

def startdate = (issue.getCustomFieldValue(plannedStartDate)as Timestamp).toLocalDateTime().toLocalDate()

def enddate = (issue.getCustomFieldValue(otherDate) as Timestamp).toLocalDateTime().toLocalDate()

if (enddate.isBefore(startdate)) {
return null
}

def weekend = EnumSet.of(DayOfWeek.SATURDAY, DayOfWeek.SUNDAY)
def workingDays = 0

while (startdate.isBefore(enddate)) {
if (!(startdate.dayOfWeek in weekend)) {
workingDays += 1
}

startdate = startdate.plusDays(1)
}

return workingDays 

 

This will return 6 when using the values you have used. What you were doing wrong was the following

if (!enddate.isBefore(startdate)) {
return null
}

 enddate in your case was after startdate so you we always entering this if statement and returning null.

And with the while loop I started with the start date and worked my was forwards instead of what you were attempting to do which was to go backwards

Amar April 5, 2019

Thank you so much Thomas for clarification :).

Code is working fine in my system as well. :)

 

Appreciated your efforts :)

0 votes
Tom _Automation Consultants_
Atlassian Partner
April 5, 2019 edited
ComponentAccessor.customFieldManager.getCustomFieldObject("customfield_16160")

needs to become 

ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_16160")

 

Amar April 5, 2019

Thank you Thomas for looking in to code.

 

However after changing the ComponentAccessor method, still I am getting Null value.

 

please suggest is any alternative or correct script.

Tom _Automation Consultants_
Atlassian Partner
April 5, 2019

I'm currently replicating the problem in my local instance, will get back to you shortly

0 votes
Ojase
Contributor
April 5, 2019 edited

Use script console and logger to debug the script. I don't see anything wrong with your script.

import com.atlassian.jira.component.ComponentAccessor
import java.sql.Timestamp
import java.time.DayOfWeek
import java.text.SimpleDateFormat;
import org.apache.log4j.Category
def Category log = Category.getInstance("com.onresolve.jira.groovy")
log.setLevel(org.apache.log4j.Level.DEBUG)
def issueManager = ComponentAccessor.getIssueManager()
def issue = issueManager.getIssueObject("ABC-123") // provide issue key here
def Plannedstartdate=ComponentAccessor.customFieldManager.getCustomFieldObject("duedate")
def dateB=ComponentAccessor.customFieldManager.getCustomFieldObject("customfield_12801")
log.info(issue.getDueDate())log.info(issue.getCustomFieldValue(dateB))

if (!issue.getDueDate() || !issue.getCustomFieldValue(dateB))
{
return null}

def enddate = (issue.getDueDate()as Timestamp).toLocalDateTime().toLocalDate()
def startdate = (issue.getCustomFieldValue(dateB) as Timestamp).toLocalDateTime().toLocalDate()

log.info(startdate)
log.info(enddate)

if (!enddate.isBefore(startdate))
{
    log.info('')return null}

def weekend = EnumSet.of(DayOfWeek.SATURDAY, DayOfWeek.SUNDAY)
def workingDays = 0
while (enddate.isBefore(startdate))
{
    log.info(enddate.dayOfWeek)
if (!(enddate.dayOfWeek in weekend))
{
workingDays += 1}

enddate = enddate.plusDays(1)
}
log.info('wd'+workingDays)

 

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, atlassian government cloud, fedramp, webinar, register for webinar, atlassian cloud webinar, fedramp moderate offering, work faster with cloud

Unlocking the future with Atlassian Government Cloud ☁️

Atlassian Government Cloud has achieved FedRAMP Authorization at the Moderate level! Join our webinar to learn how you can accelerate mission success and move work forward faster in cloud, all while ensuring your critical data is secure.

Register Now
AUG Leaders

Atlassian Community Events