I'm working on an Acumatica customization where I need to retrieve the BAccount associated with the current branch. If there is no branch configured and the instance has only an organization, I need to retrieve the organization's BAccount instead. And i working in a single tenant instance
I'm currently using the following code:
PXAccess.MasterCollection.Branch currentBranch =
PXAccess.GetBranch(PXAccess.GetBranchID());
BAccount bAccount =
BAccount.PK.Find(Base, currentBranch.BAccountID);
BAccountQAExt branchQAExt =
bAccount?.GetExtension<BAccountQAExt>();
if (branchQAExt == null)
{
string msg =
"Return warehouse or location is not configured for this branchID.";
throw new PXException(msg);
}This works correctly when the organization has branches. However, when the instance has only an organization and no branches, BAccount.PK.Find() returns null.
I verified the following through debugging and by checking the database directly:
-
PXAccess.GetBranch(PXAccess.GetBranchID())returns acurrentBranchobject. -
currentBranch.BAccountIDcontains the correctBAccountIDassociated with the organization. -
The corresponding
BAccountrecord exists in the database. -
Despite this,
BAccount.PK.Find(Base, currentBranch.BAccountID)returnsnullwhen there are no branches.
I also tried querying BAccount directly using BQL:
BAccount bAccount = SelectFrom<BAccount>
.Where<BAccount.bAccountID.IsEqual<@P.AsInt>>
.View.Select(Base, currentBranch.BAccountID);I encountered the same issue.
My questions:
-
Is there a difference in how Acumatica handles the organization's
BAccountcompared with a branch'sBAccount? -
Is
PXAccess.GetBranch()the correct way to retrieve the current organization or branch context when an organization has no branches? -
What is the recommended way to retrieve the organization's
BAccountwhen no branch exists?
Any guidance or examples would be appreciated.
Thank you!