Hi All,
Want to give you a short trick – how to extract company name from the currently logged in user:
Company Name
PX.Data.PXLogin.ExtractCompany(PX.Common.PXContext.PXIdentity.IdentityName)
Here we have 2 things:
- PXSessionContext (PXContext.PXIdentity) – System class that stores environment variables in session like User, TimeZone, Branch, Locale. Actually this is used for user authentication authorization. However user here is stored with a full notation – like “user@company:branch”. But we can cut a part of it using special rules.
- PXLogin – set of tools that can help to login user and use username. In our case it can extract company name right from full identity name.
Company ID
PX.Data.Update.PXInstanceHelper.CurrentCompany
Here we have an PXInstanceHelper class that helps to get some environment variables, like currently logged in company, site folder, IIS version, Database Name, IP Address.
Hope it helps!
Hi Sergey,
I’m trying to add custom field which will be defaulted to the current CompanyKey value
I can get company name using [PXDefault(typeof(AccessInfo.companyName))] but I need company key
Can you advise on this?
Andrey, please check here: https://asiablog.acumatica.com/2018/11/get-current-company.html
Method #1 doesn’t work if result of PX.Common.PXContext.PXIdentity.IdentityName is just user name. In my tests it was just “admin” without company name. Is there more reliable way? Thank you
Mykhaylo, please note that if there is only One company, that username is displayed without company.
In case of multiple companies, than user name will have a company name in the end.
Sergey, thanks for clarifications. But how to get name of current company in case of One company?! It’s a legal case, right?! Please would you suggest?
Mykhaylo, could you please give me the reason and scenario why you need company name, I’ll help you with the best approach.
Sergey,
You want too much 🙂 What do you think about this solution:
string companyName = PX.Data.PXLogin.ExtractCompany(PX.Common.PXContext.PXIdentity.IdentityName);
if (string.IsNullOrEmpty(companyName))
{
var key = PX.Data.Update.PXInstanceHelper.CurrentCompany;
UPCompany item = null;
foreach (UPCompany info in PXCompanyHelper.SelectCompanies())
{
if (info.CompanyID == key)
{
item = info;
break;
}
}
if (item != null)
companyName = item.LoginName;
}
At least it worked for me.
Thank you,
Mykhaylo Makarkin
Or even this:
string companyName = Base.Accessinfo.CompanyName;
“Base.Accessinfo.CompanyName” and “item.LoginName” will give you the different result. That is why I’m asking
LoginName – is the company name how it is displayed on login page
CompanyName – is how company that is currently selected in the business logic. Like branch.
If you want to login to Acumatica. You have to use LoginName how it is described here.