Skip to main content

Notifications

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 404
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?
  • Verified answer
    Waed Ayyad Profile Picture
    8,228 Super User 2025 Season 1 on at
    How to get the requester department using x++
    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
  • Waed Ayyad Profile Picture
    8,228 Super User 2025 Season 1 on at
    How to get the requester department using x++
    Hi,
     
    PO requester is Requester field on PurchTable.
     
  • D365FO DEV Profile Picture
    404 on at
    How to get the requester department using x++
    can you tell me what is PORequester in 
     
     
    hcmPositionWorkerAssignment  = HcmPositionWorkerAssignment::getActivePositionWorkerAssignment(POrequester)
    also i have debugged the values, they are all coming null
  • Suggested answer
    Waed Ayyad Profile Picture
    8,228 Super User 2025 Season 1 on at
    How to get the requester department using x++
    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
  • Waed Ayyad Profile Picture
    8,228 Super User 2025 Season 1 on at
    How to get the requester department using x++
    Hi,
     
    Yes, you should add OMOperatingUnitType. Can you check the value of the HcmWorkerpositionAssignment and HcmPositionDetail ?
     
     
    Thanks,
    Waed Ayyad
  • D365FO DEV Profile Picture
    404 on at
    How to get the requester department using x++
    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
    8,228 Super User 2025 Season 1 on at
    How to get the requester department using x++
    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
    404 on at
    How to get the requester department using x++
    Hi wael,
     
    i tried your code but its not working
  • Suggested answer
    Waed Ayyad Profile Picture
    8,228 Super User 2025 Season 1 on at
    How to get the requester department using x++
    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
     

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,095 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,866 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Product updates

Dynamics 365 release plans