Solved

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

  • 9 February 2022
  • 2 replies
  • 466 views

Userlevel 6
Badge +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

icon

Best answer by Dmitrii Naumov 9 February 2022, 00:52

View original

2 replies

Userlevel 7
Badge +5

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.

Userlevel 6
Badge +3

@Dmitrii Naumov 

“Everything else looks good.”

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

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved