How to Log Work in an iteration in scriptrunner

Retmer van Dongen September 19, 2017

I have a script in which I want to add multiple work log entries to an issue in an iteration. Part of the script looks like this:

 

DateTime dtLogWork = dtPeriodStart
Calendar calendar = Calendar.getInstance()
WorklogImpl2 objWorklog
Worklog objWorklogCreated
Long lngRemainingEstimate = budgetIssueStart.getEstimate()
Long i = 1
while (dtLogWork <= dtPeriodEnd && i < 31L) {
logText += "\nLog Work iteration " + i.toString()

// make sure the budgetIssue points to the right Budget
budgetIssue = budgetIssueStart
if (!lngYearStart.equals(lngYearEnd)) {
if (dtLogWork.getYear().toLong() == lngYearEnd) {
budgetIssue = budgetIssueEnd
lngRemainingEstimate = budgetIssueEnd.getEstimate()
}
}

cfHoursPerDay = customFieldManager.getCustomFieldObjects(budgetIssue).findByName("Hours per Day")
Double dblHoursPerDay = (Double) budgetIssue.getCustomFieldValue(cfHoursPerDay)
if (!dblHoursPerDay) {
dblHoursPerDay = 8
}

Double dblTimeSpent = dblHoursPerDay * 3600L
Long lngTimeSpent = dblTimeSpent.longValue()
lngRemainingEstimate -= lngTimeSpent

objWorklog = new WorklogImpl2(budgetIssue, null, usrEmployee.getKey(), changedIssue.getSummary(), dtLogWork.toDate(), null, null, lngTimeSpent, null)
objWorklogCreated = worklogManager.create(budgetIssue.getReporter(), objWorklog, lngRemainingEstimate, false)

logText += "\nLogged " + dblHoursPerDay.toString() + " hours (" + lngTimeSpent + "ms) on " + dtLogWork.toDate().toString()
logText += "\nRemaining estimate " + lngRemainingEstimate.toString()
logText += "\nNew Worklog.timespent: " + objWorklogCreated.getTimeSpent().toString()
logText += "\nBudgetissue.remaining estimate: " + budgetIssue.getEstimate().toString()

dtLogWork = dtLogWork.plusDays(1)
i++
}


The Log entries are created but for an example where I create three log entries with each 5 hours the end result is that the original and remaining estimate are correct but the Logged field only shows 5 hours in stead of 15.

Bildschirmfoto 2017-09-19 um 09.58.40.png

In the Work Log of the issue everything looks alright:

Bildschirmfoto 2017-09-19 um 09.59.49.png

In the history however something strange is happening. Only for the first iteration the Time Spent is changed (sort order is last updates on top)

 

Bildschirmfoto 2017-09-19 um 09.59.58.png

 

Anybody an idea if this is a bug or that the script is not correct?

Jira 7.4.2 and scriptrunner 5.0.14

 

 

1 answer

2 votes
Retmer van Dongen September 19, 2017

Found the answer finally. During the loop the issue where the work is logged on should be reloaded every time.

 

DateTime dtLogWork = dtPeriodStart
Calendar calendar = Calendar.getInstance()
WorklogImpl2 objWorklog
Worklog objWorklogCreated
Long lngRemainingEstimate = budgetIssueStart.getEstimate()
Long i = 1
while (dtLogWork <= dtPeriodEnd && i < 31L) {
logText += "\nLog Work iteration " + i.toString()

// make sure the budgetIssue points to the right Budget
budgetIssue = budgetIssueStart
if (!lngYearStart.equals(lngYearEnd)) {
if (dtLogWork.getYear().toLong() == lngYearEnd) {
budgetIssue = budgetIssueEnd
lngRemainingEstimate = budgetIssueEnd.getEstimate()
}
}

cfHoursPerDay = customFieldManager.getCustomFieldObjects(budgetIssue).findByName("Hours per Day")
Double dblHoursPerDay = (Double) budgetIssue.getCustomFieldValue(cfHoursPerDay)
if (!dblHoursPerDay) {
dblHoursPerDay = 8
}

Double dblTimeSpent = dblHoursPerDay * 3600L
Long lngTimeSpent = dblTimeSpent.longValue()
lngRemainingEstimate -= lngTimeSpent

// get a new instance of the budget issue
budgetIssue = issueManager.getIssueObject(budgetIssue.getId())


objWorklog = new WorklogImpl2(budgetIssue, null, usrEmployee.getKey(), changedIssue.getSummary(), dtLogWork.toDate(), null, null, lngTimeSpent, null)
objWorklogCreated = worklogManager.create(budgetIssue.getReporter(), objWorklog, lngRemainingEstimate, false)

logText += "\nLogged " + dblHoursPerDay.toString() + " hours (" + lngTimeSpent + "ms) on " + dtLogWork.toDate().toString()
logText += "\nRemaining estimate " + lngRemainingEstimate.toString()
logText += "\nNew Worklog.timespent: " + objWorklogCreated.getTimeSpent().toString()
logText += "\nBudgetissue.remaining estimate: " + budgetIssue.getEstimate().toString()

dtLogWork = dtLogWork.plusDays(1)
i++
}

Suggest an answer

Log in or Sign up to answer