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
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
ComponentAccessor.customFieldManager.getCustomFieldObject("customfield_16160")
needs to become
ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_16160")
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I'm currently replicating the problem in my local instance, will get back to you shortly
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 NowOnline forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.