The script searches for worklogs based on certain parameters and returns the results of the search by looping through the worklogs.

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.tempoplugin.core.datetime.api.TempoDate
import com.tempoplugin.worklog.v4.rest.InputWorklogsFactory
import com.tempoplugin.worklog.v4.rest.TimesheetWorklogBean
import com.tempoplugin.worklog.v4.services.WorklogService

import com.tempoplugin.worklog.v4.model.DateAggregatedWork
import com.tempoplugin.worklog.v4.model.IssueExpandParams
import com.tempoplugin.worklog.v4.model.TempoWorklog
import com.tempoplugin.worklog.v4.model.TempoWorklogIssueImpl
import com.tempoplugin.worklog.v4.model.UserAggregatedWork
import com.tempoplugin.worklog.v4.model.WorklogDescription
import com.tempoplugin.worklog.v4.model.WorklogDescriptionImpl
import com.tempoplugin.worklog.v4.model.WorklogSearchParams
import com.tempoplugin.worklog.v4.services.validation.WorklogPermissionCheckService
import com.tempoplugin.worklog.v4.services.search.WorklogSearchService

import org.apache.log4j.Level
import org.apache.log4j.Logger
import com.tempoplugin.core.workattribute.api.WorkAttributeService
import com.tempoplugin.core.workattribute.api.WorkAttributeValueService
import com.atlassian.jira.issue.worklog.Worklog
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField

import org.joda.time.Days
import org.joda.time.DateTime
import org.joda.time.LocalDate
import org.joda.time.Weeks

def myLog = Logger.getLogger("com.onresolve.jira.groovy")
myLog.setLevel(Level.DEBUG)

def IssueManager = ComponentAccessor.getIssueManager()
//custom Field manager not needed in the sample but might be used to extract Jira issue data on the worklog
def customFieldManager = ComponentAccessor.customFieldManager

@WithPlugin("is.origo.jira.tempo-plugin")
@PluginModule
WorkAttributeService workAttributeService
@PluginModule
WorkAttributeValueService workAttributeValueService
@PluginModule
WorklogSearchService worklogSearchService

//get worklog attributes 
def attribute = workAttributeService.getWorkAttributeByKey("_WorklogCategory_").returnedValue
def billedsecondsattribute = workAttributeService.getWorkAttributeByKey("Tempo.WorklogBilledHours").returnedValue

//Setting the worklog search parameters
WorklogSearchParams worklogSearchParams = new WorklogSearchParams()
//worklogSearchParams.withAccountKeys("111-111")
worklogSearchParams.withFrom(LocalDate.parse("2020-12-01"))
worklogSearchParams.withTo(LocalDate.parse("2021-01-31"))
worklogSearchParams.withWorkerKeys("john","bob")

IssueExpandParams issueExpandParams = new IssueExpandParams.Builder().build()

def worklogs = worklogSearchService.findWorklogs(worklogSearchParams, issueExpandParams)
//myLog.info("Returned worklogs: " + worklogs)

//Looping through the returned worklogs by the search
worklogs.each { 
TempoWorklog tempoworklog ->
def worklogid = tempoworklog.getId()
def worklogauthor = tempoworklog.getWorkerKey() 
def worklogcreated = tempoworklog.getCreated()
//other allowed methods
//getUpdaterKey() ,getComment() , getCreated(), getUpdated()
//getStartDate(),getTimeSpentSeconds(), getIssueId()
   
def issuedetails = tempoworklog.getIssue()
//returns the Jira issue object on the worklog
def IssueKey = issuedetails.getKey()
    
myLog.info("Worklog ID: " + worklogid + " was created by: " + worklogauthor + " on: " + worklogcreated + " logged on Issue: "  + IssueKey)
//get value from worklog attributes (including billable seconds)
def attributeValue = workAttributeValueService.getWorkAttributeValueByWorklogAndWorkAttribute(worklogid, attribute.id).returnedValue
if(attributeValue) {myLog.info("WorklogAttributevalue of the worklog: "  + attributeValue.value)}
else {myLog.info("Worklog attribute not defined on the worklog")}
def billedSecondsValue = workAttributeValueService.getWorkAttributeValueByWorklogAndWorkAttribute(worklogid, billedsecondsattribute.id).returnedValue
myLog.info("Billed Seconds on worklog: "  + billedSecondsValue?.value)   
}

Further search parameters can be defined.

from,
to,
updatedFrom,
workerKeys,
issueIds,
issueKeys,
projectIds,
projectKeys,
teamIds,
roleIds,
accountIds,
accountKeys,
filterIds,
customerIds,
categoryIds,
categoryTypeIds,
epicKeys

In Version 11.1 of Tempo Timesheets the IssueExpandParams class was removed. For Tempo Timesheets Versions 11.1 and later, remove the following lines from your code:

import com.tempoplugin.worklog.v4.model.IssueExpandParams
...
IssueExpandParams issueExpandParams = new IssueExpandParams.Builder().build()

And you will need to amend the findWorklogs() function from

def worklogs = worklogSearchService.findWorklogs(worklogSearchParams, issueExpandParams)

to

def worklogs = worklogSearchService.findWorklogs(worklogSearchParams)