Skip to main content
Answer

OwnerID has changed to int in 2021. What is the best way to get the OwnerID from the current UserID

  • February 8, 2022
  • 2 replies
  • 647 views

Joe Schmucker
Captain II
Forum|alt.badge.img+3

When a user creates a new contact, my customer wants the Owner field to be defaulted to the current user account.

I’ve written code to do this, but I am afraid the Acumatica police will come knocking any day now.

I’m upgrading someone else’s code and in 2020R1, they simply set the OwnerID field to the current UserID.  The OwnerID field is now int, not GUID so the code was broken.

First, I am using an obsolete call to PXAccess.GetUserID().  I use that ID in a Fluent BQL statement to get the DefContactID from the BAccount table by joining it to the EPEmployee table where the EPEmployee is the UserID.

It isn’t pretty but it works.  I am supposed to use ICurrentUserInformationProvider to get the UserID but I don’t know how to implement it.  

This is the code I am using:

        protected void Contact_ContactID_FieldDefaulting(PXCache cache, PXFieldDefaultingEventArgs e)
        {
            var row = (Contact)e.Row;
            Contact crcontact = e.Row as Contact;
            if (row.ContactID == null)
            {
                var myID = PXAccess.GetUserID();

                BAccount account = SelectFrom<BAccount>.InnerJoin<EPEmployee>.On<EPEmployee.bAccountID.IsEqual<BAccount.bAccountID>>.Where<EPEmployee.userID.IsEqual<@P.AsGuid>>
                     .View.Select(Base, myID);

                if (account != null)
                {
                    row.OwnerID = account.DefContactID;
                }
            }
        }
 

If anyone wants to give me advice on how to improve this, that would be great.

Thank you,

Joe

Best answer by Dmitrii Naumov

Hello Joe, 

You can get userID like that

var currentUser = CommonServiceLocator.ServiceLocator.Current.GetInstance<ICurrentUserInformationProvider>().GetUserID();

You’ll need to add a reference to CommonServiceLocator.dll that you can find in the Acumatica bin.

 

Everything else looks good.

2 replies

Dmitrii Naumov
Acumatica Moderator
Forum|alt.badge.img+7
  • Acumatica Moderator
  • Answer
  • February 8, 2022

Hello Joe, 

You can get userID like that

var currentUser = CommonServiceLocator.ServiceLocator.Current.GetInstance<ICurrentUserInformationProvider>().GetUserID();

You’ll need to add a reference to CommonServiceLocator.dll that you can find in the Acumatica bin.

 

Everything else looks good.


Joe Schmucker
Captain II
Forum|alt.badge.img+3
  • Author
  • Captain II
  • February 9, 2022

@Dmitrii Naumov 

“Everything else looks good.”

THANK YOU!  what's the first time i've ever heard anyone say that!  :grinning: