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

(3) ShareShare
ReportReport
Posted on by 329
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:
I have the same question (0)
  • Suggested answer
    Waed Ayyad Profile Picture
    9,039 Super User 2025 Season 2 on at
    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

  • Verified answer
    Martin Dráb Profile Picture
    237,904 Most Valuable Professional on at
    Use HcmWorker::userId2Worker().
     
    By the way, no DataAreaId is involved, because all the tables are shared across companies.
  • Superbunny Profile Picture
    329 on at
    @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!
  • Waed Ayyad Profile Picture
    9,039 Super User 2025 Season 2 on at
     
    Did you try my suggestion? It should give you the same result.
     
    Thanks,
    Waed Ayyad
  • Superbunny Profile Picture
    329 on at
    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.

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
Martin Dráb Profile Picture

Martin Dráb 544 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 250 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans