Skip to main content
X++ Development · · 2 min read

Get Last Worked Date for an Employee

 Retrieve an employee’s last worked date in D365 F&O using X++ and related payroll and employment records. 

Ahmad Hassan

Ahmad Hassan

Dynamics 365 F&O Consultant

Share
Get Last Worked Date for an Employee

This method retrieves the last worked date for an employee using their personnel number. It identifies the employee, related payroll pay statement, pay period end date, and employment records before retrieving the latest LastDateWorked from HcmEmploymentDetail. The resulting date is converted and returned as a ValidToDate.

X++ Code

public static ValidToDate GetLastDateEmployee(HcmPersonnelNumberId _personalNum)
{
     HcmWorker                   hcmWorkers;
     HcmEmployment               hcmEmployments;
     PayrollPayStatement         payrollPayStatement;
     HcmEmploymentDetail         hcmEmploymentDetail;
     HcmPositionWorkerAssignment hcmPositionWorkerAssignment;
     date                        lastDateEmployee, LastDate;
     lastDateEmployee          = dateNull(); // Default to null date

     select hcmworkers where hcmworkers.personnelnumber == _personalNum;
     select payrollPayStatement where payrollPayStatement.Worker == hcmworkers.RecId;

     LastDate = PayrollPayPeriod::find(payrollPayStatement.PayPeriod).PeriodEndDate;

     select ValidTimeState(LastDate) hcmPositionWorkerAssignment
             order by ValidTo desc
             where hcmPositionWorkerAssignment.Worker == hcmworkers.RecId;

     select ValidTimeState(LastDate) hcmEmployments order by hcmEmployments.ValidTo
             where hcmEmployments.Worker   ==  hcmWorkers.RecId;

     // Check if the employee is inactive
     //added rejina
     //  if(hcmPositionWorkerAssignment.status() != HcmPositionAssignmentStatus::Active)
     //  {
     select hcmEmploymentDetail
             order by hcmEmploymentDetail.LastDateWorked desc
             where hcmEmploymentDetail.Employment == hcmEmployments.RecId;

     lastDateEmployee = DateTimeUtil::date(hcmEmploymentDetail.LastDateWorked);
     //  }
     return lastDateEmployee;
}

Process Flow

The method receives an employee personnel number and retrieves the corresponding HcmWorker and payroll pay statement.

It determines the relevant payroll period end date and retrieves position assignment and employment records using valid-time state queries.

The latest LastDateWorked is retrieved from HcmEmploymentDetail and converted to a date.

The resulting last worked date is returned by the method.

c5122877-6145-4155-8cc0-0d91615c03ef.png

Figure: Flow of retrieving an employee's last worked date from payroll and employment records.

Found this useful?

Share it with your network

Ahmad Hassan

Written by

Ahmad Hassan

Microsoft Dynamics 365 & Power Platform blogger.

More about me

Related Posts

Comments

Leave a comment