cancel
Showing results for 
Search instead for 
Did you mean: 

Timesheet via DI API

yann_mabillard
Explorer
0 Kudos

Hi everybody,

We are using SAP B1 9.2 PL08 and we have a macro done in vba (excel) to interact with sap using DI API (sapbobscom). It works well. Now I would like to use the TimeSheet object to register user's time. I found an example on the web (https://blogs.sap.com/2017/08/01/time-sheet-in-sap-business-one-sdk/) and try to translate into vba, wihout success.

Hereafter my code :

    Dim oCompanyService As SAPbobsCOM.CompanyService
    Dim oTimeSheetService As SAPbobsCOM.ProjectManagementTimeSheetService
    Dim oTimeSheet As SAPbobsCOM.PM_TimeSheetData
    Dim oTimeSheetLine As SAPbobsCOM.PM_TimeSheetLineData
    Dim oTimeSheetParam As SAPbobsCOM.PM_TimeSheetParams
    Dim StartTime As Date
    Dim EndTime As Date
    Dim breakTime As Date
    Dim NonBillableTime As Date
    
    ConnectionSQLSAPOpen
    
    Set oCompanyService = oCompanySAP.GetCompanyService
    Set oTimeSheetService = oCompanyService.GetBusinessService(ProjectManagementTimeSheetService)
    Set oTimeSheet = oTimeSheetService.GetDataInterface(pmtssPM_TimeSheetData)
    Set oTimeSheetLine = oTimeSheetService.GetDataInterface(pmtssPM_TimeSheetLineData)
    Set oTimeSheetParam = oTimeSheetService.GetDataInterface(pmtssPM_TimeSheetParams)
    
    oTimeSheet.TimeSheetType = SAPbobsCOM.TimeSheetTypeEnum.tsh_Employee
    oTimeSheet.UserId = 2
    
    
    StartTime = VBA.DateAdd("h", 7, VBA.Int(Now)) 'start a 7.00
    EndTime = VBA.DateAdd("n", 90, StartTime) 'add 90 minutes
    breakTime = VBA.DateAdd("n", 45, VBA.Int(Now)) 'pause  45 minutes


    NonBillableTime = VBA.DateAdd("n", 15, VBA.Int(Now))
    oTimeSheetLine.Date = Now
    oTimeSheetLine.StartTime = StartTime
    oTimeSheetLine.EndTime = EndTime
    oTimeSheetLine.Break = breakTime
    oTimeSheetLine.NonBillableTime = NonBillableTime
    oTimeSheetLine.ActivityType = 1
    oTimeSheetLine.CostCenter = ""
    oTimeSheetLine.FinancialProject = "090"
    
    oTimeSheetLine = oTimeSheet.PM_TimeSheetLineDataCollection.Add()
    
    oTimeSheetParam = oTimeSheetService.AddTimeSheet(oTimeSheet)
    
    ConnectionSQLSAPClose

We program stops at 'oTimeSheetLine = oTimeSheet.PM_TimeSheetLineDataCollection.Add()'.


Could you please help me ?


Kind regards

Accepted Solutions (1)

Accepted Solutions (1)

dan_jordan
Explorer
0 Kudos

In lieu of the first Set oTimesheetLine statement, put what you had at the end, except use the "Set" keyword instead:

Set oTimeSheetLine = oTimeSheet.PM_TimeSheetLineDataCollection.Add()

Also make sure to use the "Set" keyword with the call to AddTimeSheet:

Set oTimeSheetParam = oTimeSheetService.AddTimeSheet(oTimeSheet)
yann_mabillard
Explorer
0 Kudos

Thank you Dan, it works as a charm 😉

Answers (0)