Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Answered

How to list employees within the same department?

Posted on by 58

I have a requirement to show employees within the same department, in bellow code I get the current user department and store it in currentuserdepartment.

str 50 currentuserdepartment=DirPartyTable::findRec(HCMWorkerHirarchy_View::findRecId(HcmWorker::userId2Worker(curUserId())).Department,false,DirPartyType::OperatingUnit).Name;

For example the result of currentuserdepartment is 'Finance', how can I list everyone belonging to finance department.

Any help is appreciated. 

  • EagleClaws Profile Picture
    EagleClaws 58 on at
    RE: How to list employees within the same department?

    Hi Ludwig, thanks for your response.

    basically I wanted to filter a forms data according according to current user, we had a requirement to allow a user to see ONLY his and his coworker records entered by them. previously a user with access to a form was able to see records from all departments.

    I used the above code in int method of forms data source to filter records accordingly.

  • Suggested answer
    EagleClaws Profile Picture
    EagleClaws 58 on at
    RE: How to list employees within the same department?

    Thank you Nikolaos for helping me out, you were right, I had to create make some changes in query I was firing on the database to make it work better.

    With the following code I was able to print the names of employees within the same department, the code checks the current user who is active, then prints his colleague who are in the same department:

    static void WorksCR5Job114(Args _args)
    {
    
        HcmPositionWorkerAssignment     positionWorkerAssignment;
        HcmPositionDetail               positionDetail;
        HcmPositionDuration             positionDuration;
        OMOperatingUnit                 operatingUnit;
        HcmWorkerRecId _workerRecId;
        HcmPersonnelNumberId    _personnelNumber ;
        HcmWorker  HcmWorker;
    
    
        str 50 currentuserdepartment;
        ;
    
         _personnelNumber=HcmWorker::find(HcmWorker::userId2Worker(curUserId())).PersonnelNumber;
    
        _workerRecId=HcmWorker::findByPersonnelNumber(_personnelNumber).RecId;
    
        //Query to store current user department:
             select recid from HcmWorker
                        where HcmWorker.RecId==_workerRecId
        join operatingUnit
            join Department, Position from positionDetail
            where positionDetail.Department == operatingUnit.RecId
                join Position, Worker from positionWorkerAssignment
                where positionWorkerAssignment.Position == positionDetail.Position &&
                    positionWorkerAssignment.Worker == _workerRecId
                exists join Position from positionDuration
                    where positionDuration.Position == positionDetail.Position;
                            currentuserdepartment=operatingUnit.Name;
            //The above query stops here after storing the current user department to be used with the next query
    
        while select recid from HcmWorker
        join operatingUnit
            join Department, Position from positionDetail
            where positionDetail.Department == operatingUnit.RecId
                join Position, Worker from positionWorkerAssignment
                where positionWorkerAssignment.Position == positionDetail.Position &&
                    positionWorkerAssignment.Worker == HcmWorker.RecId
                exists join Position from positionDuration
                    where positionDuration.Position == positionDetail.Position &&
                            operatingUnit.Name==currentuserdepartment
    
         print HcmWorker::find(hcmWorker.RecId).name();
            pause;
    }

  • Suggested answer
    Ludwig Reinhard Profile Picture
    Ludwig Reinhard Microsoft Employee on at
    RE: How to list employees within the same department?

    Hi,

    Why do you have / want to code that?

    Can't you simply filter for the respective records in the employee table and export those filtered results e.g. to excel to get the employees you are looking for?

    Wouldn't that be sufficient?

    Best regards,

    Ludwig

  • Verified answer
    nmaenpaa Profile Picture
    nmaenpaa 101,156 on at
    RE: How to list employees within the same department?

    You should put the conditions in the select statement, otherwise it's indeed slow.

    Now your query loops all workers, and for each of them it asks "is this worker part of Finance department?".

    Also, you have unnecessary calls for "HcmWorker::find"is definetely not needed since you already have a HcmWorker at hand inside the while statement.

    So, first of all, let's clean out those calls.

    static void Job114CR5(Args _args)
    {
       HcmWorker hcmWorker;
       while select hcmWorker 
       {
           if(hcmWorker.getdepartmentname()=='Finance')
           {
              info(hcmWorker.name());
           }
       }
    }

    Then, we want to improve it further since you still do a lot of unnecessary work and lot of database calls, when you would like to have just one query in the database. You should formulate this question as part of the select statement. So, take a look what's inside HcmWorker.getDepartmentName() and move that logic into your select statement.

    Eventually you want to have something like this:

    while select hcmWorker
        where departmentName == "Finance" // You need to formulate here some condition
    {
        info(hcmWorker.name());
    }

  • Suggested answer
    EagleClaws Profile Picture
    EagleClaws 58 on at
    RE: How to list employees within the same department?

    Thank you for the fast reply Nikolaos,

    I was able list the names of employees with your help through this code:

    static void Job114CR5(Args _args)

    {

       HcmWorker hcmWorker;

     while select  RecId from hcmWorker

           if( HcmWorker::find(hcmWorker.RecId).getdepartmentname()=='Finance')

       {

           print HcmWorker::find(hcmWorker.RecId).name();

           pause;

       }

    }

    However it's extremely slow, it takes a lot of time to return a single employee name.

  • nmaenpaa Profile Picture
    nmaenpaa 101,156 on at
    RE: How to list employees within the same department?

    Since you already know all the relevant links between the records, it should be fairly easy for you to write a select statement that iterates all users where department is "x".

    Just look into these methods, and then combine logic from the methods into one select statement:

    - DirPartyTable::findRec

    - HCMWorkerHierarchy_View::findRecId

    - HcmWorker::userid2Worker

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans