How to Log Work in an iteration in scriptrunner

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

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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Sunday in Agility

You asked for it, so we delivered: images on issues have arrived

A picture tells a thousand words. And agility boards have just released their latest feature: cover images on issues – so now your board can tell a story at first glance. Upload attachmen...

921 views 3 12
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you