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 find a HcmWorkerRecId via a UserInfo.Id

(4) ShareShare
ReportReport
Posted on by 240
I am trying to create the following functionality:
 
When a purchase line is inserted, we want to copy the user in the createdBy field of the salesline to the requester field of the purchline.
 
For this I checked the table relations and created the following code:
 
 

        select firstonly * from salesLine
            where this.InventRefTransId == salesLine.InventTransId;

        if(salesLine.RecId)
        {
            select firstonly Id from UserInfo where salesLine.createdBy == UserInfo.Id;
            if(UserInfo.Id)
            {
                PartyIProvider partyProvider = PartyProviderFactory::getPartyProvider();           
                if (partyProvider)
                {
                    str fullName = partyProvider.dirPersonUserId2Name(UserInfo.id);

                    select * from dirPartyNameView where Name == fullName
                        join RecId from hcmWorker where HcmWorker.Person == dirPartyNameView.RecId;
                }                
                this.Requester = hcmWorker.RecId;
                this.update();
            }
        }
The problem is that the dirPartyNameView does not return any data, despite the data existing. 
(in SQL the query does work).
 
I also tried the following queries: (since i thought the problem could be caused by the instanceRelationsType column, as I am looking for a dirPerson and not a dirParty)

select * from dirPartyNameView where Name == fullName;
select * from dirPersonEntity where Name == fullName;
select  * from dirPartyTable where Name == fullName;
 
Both of these queries give me the correct data in SQL Server when I enter the fullname field with the value that is used in D365, but I keep getting no data in D365. 
These views also contain no dataareaId which could influence whether data is returned.
 
How is it possible that I can not find the correct records via the application?
Categories:
  • Superbunny Profile Picture
    240 on at
    How to find a HcmWorkerRecId via a UserInfo.Id
    Hey Waed,
     
    Is your suggestion not simply returning the worker of the currently logged in person?
     
    I am specifically trying to find the HcmWorker record which corresponds to the found CreatedBy column of a salesLine, which points to the UserInfo table.
  • Waed Ayyad Profile Picture
    7,851 Super User 2025 Season 1 on at
    How to find a HcmWorkerRecId via a UserInfo.Id
     
    Did you try my suggestion? It should give you the same result.
     
    Thanks,
    Waed Ayyad
  • Superbunny Profile Picture
    240 on at
    How to find a HcmWorkerRecId via a UserInfo.Id
    @Martin Dráb
     
     
    Hello Martin,
     
    Thank you, that did it! It does certainly not explain why the other queries do not work, but I guess that answer could only be found by swimming through what query the kernel makes.
     
    This solves my issue, so I will mark your answer!
  • Verified answer
    Martin Dráb Profile Picture
    231,837 Most Valuable Professional on at
    How to find a HcmWorkerRecId via a UserInfo.Id
    Use HcmWorker::userId2Worker().
     
    By the way, no DataAreaId is involved, because all the tables are shared across companies.
  • Suggested answer
    Waed Ayyad Profile Picture
    7,851 Super User 2025 Season 1 on at
    How to find a HcmWorkerRecId via a UserInfo.Id
    Hi,
     
    You can use this standard static method to get current worker.
     
    HcmWorkerLookup::currentWorker();
    select firstonly forupdate * from salesLine
        where this.InventRefTransId == salesLine.InventTransId;
    
     if(salesLine.RecId)
      {    
          ttsbegin;
    
          this.Requester = HcmWorkerLookup::currentWorker();
          this.update();
    
          ttscommit;     
               
       }

     
     

    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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February 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... 293,025 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,837 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans