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

Get Department for a Past Employee

 Retrieve a past employee’s department in D365 F&O using X++ and historical position assignment data from HcmWorker, HcmPositionDetail, and OMOperatingUnit. 

Ahmad Hassan

Ahmad Hassan

Dynamics 365 F&O Consultant

Share
Get Department for a Past Employee

This method retrieves the department associated with a past employee using their personnel number. It identifies the employee’s payroll period end date, retrieves the relevant historical position assignment, and uses the position to locate the corresponding department in OMOperatingUnit. The department name is then returned as the result.

X++ Code

public static Name GetDepartmentOnPastEmployee(HcmPersonnelNumberId _personalNumber)
{
     HcmWorker                       hcmWorker;
     OMOperatingUnit                 oMOperatingUnit;
     HcmPositionDetail               hcmPositionDetail;
     PayrollPayStatement             payrollPayStatement;
     HcmPositionWorkerAssignment     hcmPositionWorkerAssignment;
     date                            EndDate;

     select Person, RecId from hcmWorker
         where hcmWorker.PersonnelNumber == _personalNumber;

     select payrollPayStatement
         where payrollPayStatement.Worker == hcmWorker.RecId;

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

     select ValidTimeState(EndDate) hcmPositionWorkerAssignment
         order by ValidTo desc
         where hcmPositionWorkerAssignment.Worker == hcmworker.RecId;

     // Retrieve the department details from OMOperatingUnit
     // by joining it with HcmPositionDetail based on the position assignment
     select * from oMOperatingUnit
         join hcmPositionDetail
         order by ValidTo desc
         where hcmPositionDetail.Department == oMOperatingUnit.RecId
&&  hcmPositionDetail.Position   == hcmPositionWorkerAssignment.Position;

     return oMOperatingUnit.Name;
}

Process Flow

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

It determines the payroll period end date and retrieves the employee’s position assignment valid at that date.

The position is matched with HcmPositionDetail and OMOperatingUnit to identify the associated department.

The department name from OMOperatingUnit is returned.

b93d29f4-0fc7-406f-9a91-767ac3058a95.png

Figure: Flow of retrieving the department for a past employee using historical position information.

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