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

Notifications

Announcements

No record found.

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,039 Super User 2025 Season 2 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,039 Super User 2025 Season 2 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,039 Super User 2025 Season 2 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,039 Super User 2025 Season 2 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,039 Super User 2025 Season 2 on at
    Hi,
     
    PO requester is Requester field on PurchTable.
     
  • Verified answer
    Waed Ayyad Profile Picture
    9,039 Super User 2025 Season 2 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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 467 Super User 2025 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 420 Most Valuable Professional

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 241 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans