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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

389 views 1 18
Join discussion

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