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 Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,777 views 11 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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot