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.
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.

Figure: Flow of retrieving the department for a past employee using historical position information.
Related Posts
Get Exchange Rate for Currency Conversion
Retrieve exchange rates in D365 F&O using X++ for currency conversion and exchange rate-based financial calculations.
Control Purchase Order Form Fields Based on User Security
Control Purchase Order form fields in D365 F&O using X++ based on user security and access requirements.
Check Pending Workflow Work Item for Purchase Requisition
Check pending Purchase Requisition workflow work items in D365 F&O using X++ to identify approval status and support workflow processing.