web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

How to get the requester department using x++

(1) ShareShare
ReportReport
Posted on by 406
Hello,
 
i need the x++ to retreive the department of the selected requested in my PO header,
now if u click on position it shows you the Department of this requester.
 
so anyone can provide the x++ code to make this happen?
I have the same question (0)
  • Suggested answer
    Waed Ayyad Profile Picture
    9,074 Super User 2026 Season 1 on at
    Hi,
     
    Check this code:
        HcmWorker                       hcmWorker;
        HcmPositionWorkerAssignment     hcmPositionWorkerAssignment;
        OMOperatingUnit                 oMOperatingUnit;
        HcmPositionDetail               hcmPositionDetail;
        HcmPosition                     hcmPosition;
    
        hcmPositionWorkerAssignment  = HcmPositionWorkerAssignment::getActivePositionWorkerAssignment(POrequester);
        hcmPosition                  = HcmPosition:find(hcmPositionWorkerAssignment.position);
        hcmPositionDetail            = HcmPositionDetail::findbyPosition(hcmPositionWorkerAssignment.position);
        oMOperatingUnit              = OMOperatingUnit::find(hcmPositionDetail.Department);
       
        info("Department:"+oMOperatingUnit.Name);
    Thanks,
    Waed Ayyad
    If this helped, please mark it as "Verified" for others facing the same issue
     
  • D365FO DEV Profile Picture
    406 on at
    Hi wael,
     
    i tried your code but its not working
  • Waed Ayyad Profile Picture
    9,074 Super User 2026 Season 1 on at
    Hi,
     
    It should work, did you try to trace the code? Does the requester have active position or not?  Reply to my questions in order to help you.
     
     
    Thanks,
    Waed Ayyad
     
     
     
  • D365FO DEV Profile Picture
    406 on at
    Hi wael,
     
    when i tried your code:
    i got the following error:
      'OMOperatingUnit.find' is missing argument 2 of type 'Extensible Enumeration(OMOperatingUnitType)'. 
     
    so i added the enum type:
    Now i debugged the code and the values are null
  • Waed Ayyad Profile Picture
    9,074 Super User 2026 Season 1 on at
    Hi,
     
    Yes, you should add OMOperatingUnitType. Can you check the value of the HcmWorkerpositionAssignment and HcmPositionDetail ?
     
     
    Thanks,
    Waed Ayyad
  • Suggested answer
    Waed Ayyad Profile Picture
    9,074 Super User 2026 Season 1 on at
    Hi,
     
    You can get it on another way:
    OMOperatingUnit oMOperatingUnit = HcmWorkerHelper::getPrimaryDepartment(PORequester);
    
    info(oMOperatingUnit.name);
    
    
    Thanks,
    Waed Ayyad
    If this helped, please mark it as "Verified" for others facing the same issue
  • D365FO DEV Profile Picture
    406 on at
    can you tell me what is PORequester in 
     
     
    hcmPositionWorkerAssignment  = HcmPositionWorkerAssignment::getActivePositionWorkerAssignment(POrequester)
    also i have debugged the values, they are all coming null
  • Waed Ayyad Profile Picture
    9,074 Super User 2026 Season 1 on at
    Hi,
     
    PO requester is Requester field on PurchTable.
     
  • Verified answer
    Waed Ayyad Profile Picture
    9,074 Super User 2026 Season 1 on at
    Hi,
     
    PO requester is Requester field on Purch Table. You should select the Purch Table buffer first

       HcmWorker                       hcmWorker;
        HcmPositionWorkerAssignment     hcmPositionWorkerAssignment;
        OMOperatingUnit                 oMOperatingUnit;
        HcmPositionDetail               hcmPositionDetail;
        HcmPosition                     hcmPosition;
    PurchTable purchTable;
    select Requester from purchTable where .... // Add where based on RecId or PurchTd, hcmPositionWorkerAssignment = HcmPositionWorkerAssignment::getActivePositionWorkerAssignment(purchTable.Requester); hcmPosition = HcmPosition:find(hcmPositionWorkerAssignment.position); hcmPositionDetail = HcmPositionDetail::findbyPosition(hcmPositionWorkerAssignment.position); oMOperatingUnit = OMOperatingUnit::find(hcmPositionDetail.Department, OMOperatingUnitType::OMDepartment); info("Department:"+oMOperatingUnit.Name);

    Thanks,
    Waed Ayyad
    If this helped, please mark it as "Verified" for others facing the same issue

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 521 Super User 2026 Season 1

#2
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 438

#3
Adis Profile Picture

Adis 261 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans