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.
In the Work Log of the issue everything looks alright:
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)
Anybody an idea if this is a bug or that the script is not correct?
Jira 7.4.2 and scriptrunner 5.0.14
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++
}
Online 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.