Skip to main content
Question

How to Retrieve the Current Organization's BAccount When No Branch Exists

  • October 15, 2026
  • 7 replies
  • 75 views
  • Translate All

Forum|alt.badge.img+2

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 a currentBranch object.

  • currentBranch.BAccountID contains the correct BAccountID associated with the organization.

  • The corresponding BAccount record exists in the database.

  • Despite this, BAccount.PK.Find(Base, currentBranch.BAccountID) returns null when 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:

  1. Is there a difference in how Acumatica handles the organization's BAccount compared with a branch's BAccount?

  2. Is PXAccess.GetBranch() the correct way to retrieve the current organization or branch context when an organization has no branches?

  3. What is the recommended way to retrieve the organization's BAccount when no branch exists?

Any guidance or examples would be appreciated.

Thank you!

7 replies

darylbowman
Captain II
Forum|alt.badge.img+17

Have you tried using Branch.OrganizationID instead?

Translate

KrunalDoshi
Pro I
Forum|alt.badge.img+9

Hi ​@PDharmasena10,

In your single-tenant Acumatica instance, the important distinction is between the current branch context, the organization, and the business account (BAccount) associated with either one.

Even when an organization has no branches, Acumatica can still have an organization record and its associated business account. The fact that PXAccess.GetBranch() returns an object does not necessarily mean that its BAccountID can be resolved through BAccount.PK.Find() in that configuration.

I recommend resolving the organization explicitly through Organization.OrganizationID, then using its BAccountID.

Organization organization = SelectFrom<Organization>
.Where<Organization.organizationID
.IsEqual<@P.AsInt>>
.View.Select(Base, currentBranch.OrganizationID);

int? bAccountID = organization?.BAccountID
?? currentBranch.BAccountID;

Hope this helps!

 

Translate

Dipak Nilkanth
Pro III
Forum|alt.badge.img+14

Hi ​@PDharmasena10,
could you please try below code snippet ? let us know your results.
 

int? branchID = PXAccess.GetBranchID();

YourGraph backgroundGraph =
PXGraph.CreateInstance<YourGraph>();

PX.Objects.GL.Branch currentBranch = branchID != null
? PX.Objects.GL.Branch.PK.Find(backgroundGraph, branchID)
: null;

PX.Objects.CR.BAccount bAccount = null;

if (currentBranch != null)
{
bAccount = PX.Objects.CR.BAccount.PK.Find(
backgroundGraph,
currentBranch.BAccountID);

if (bAccount == null)
{
PX.Objects.GL.DAC.Organization organization =
SelectFrom<PX.Objects.GL.DAC.Organization>
.Where<
PX.Objects.GL.DAC.Organization.organizationID
.IsEqual<@P.AsInt>>
.View.Select(
backgroundGraph,
currentBranch.OrganizationID);

if (organization != null)
{
bAccount = PX.Objects.CR.BAccount.PK.Find(
backgroundGraph,
organization.BAccountID);
}
}
}


return bAccount.BAccountID;

Hope, it helps!

Translate

Forum|alt.badge.img+2

Hi ​@Dipak Nilkanth 

Thank you. I tested your solution, but I’m still experiencing the same issue.

The code works correctly when there is a branch. However, when the organization has no branches, the code retrieves the organization correctly, but attempting to retrieve its associated BAccount using the BAccountID returns null.

bAccount = PX.Objects.CR.BAccount.PK.Find(
backgroundGraph,
organization.BAccountID);

I debugged the code and verified the BAccountID against the database. The correct BAccountID is passed in both scenarios, but BAccount.PK.Find() returns the expected record only when the BAccount is associated with a branch.
Issue happens when I log in to this CAPITAL Organization

Following are the existing data recodes in DB
 

Also, I verified that the following code returns the correct Branch object in both scenarios, including when the instance has only an organization and no branches:

PXAccess.MasterCollection.Branch currentBranch =
PXAccess.GetBranch(PXAccess.GetBranchID());
Translate

KrunalDoshi
Pro I
Forum|alt.badge.img+9

Hi ​@PDharmasena10,

In your single-tenant Acumatica instance, the important distinction is between the current branch context, the organization, and the business account (BAccount) associated with either one.

Even when an organization has no branches, Acumatica can still have an organization record and its associated business account. The fact that PXAccess.GetBranch() returns an object does not necessarily mean that its BAccountID can be resolved through BAccount.PK.Find() in that configuration.

I recommend resolving the organization explicitly through Organization.OrganizationID, then using its BAccountID.

Organization organization = SelectFrom<Organization>
.Where<Organization.organizationID
.IsEqual<@P.AsInt>>
.View.Select(Base, currentBranch.OrganizationID);

int? bAccountID = organization?.BAccountID
?? currentBranch.BAccountID;

Hope this helps!

 

Hi ​@PDharmasena10,

Have you tested with this solution? Did that worked?

Translate

Forum|alt.badge.img+2

Hi ​@KrunalDoshi , ​@darylbowman 

Yes, I tested this as well. Here is the relevant code:

if (bAccount == null)
{
PX.Objects.GL.DAC.Organization organization =
SelectFrom<PX.Objects.GL.DAC.Organization>
.Where<
PX.Objects.GL.DAC.Organization.organizationID
.IsEqual<@P.AsInt>>
.View.Select(backgroundGraph,currentBranch.OrganizationID);

if (organization != null)
{
bAccount = PX.Objects.CR.BAccount.PK.Find(backgroundGraph,organization.BAccountID);
}
}

As mentioned in my previous reply, the query successfully retrieves the correct Organization record. However, when I use its BAccountID to retrieve the associated BAccount, the result is still null when the organization has no branches.

I also verified that the BAccountID passed to BAccount.PK.Find() is correct by debugging the code and checking the database directly.

The issue seems to be specifically related to retrieving the organization's BAccount, rather than retrieving the organization itself. 

Translate

KrunalDoshi
Pro I
Forum|alt.badge.img+9

Hi ​@PDharmasena10,

I have created a new tenant without any Branch, which has below output. 

SQL output-

Here is the code I have written-

#region Event Handlers
public PXAction<SOOrder> GetBAccountID;

[PXButton(CommitChanges = true)]
[PXUIField(
DisplayName = "Get BAccountID",
MapEnableRights = PXCacheRights.Select,
MapViewRights = PXCacheRights.Select)]
protected virtual IEnumerable getBAccountID(PXAdapter adapter)
{
// Get the current branch
PXAccess.MasterCollection.Branch accessBranch =
PXAccess.GetBranch(PXAccess.GetBranchID());

if (accessBranch == null)
{
throw new PXException("Unable to determine the current branch.");
}

// Retrieve the full Branch DAC record
PX.Objects.GL.Branch currentBranch = SelectFrom<PX.Objects.GL.Branch>
.Where<PX.Objects.GL.Branch.branchID.IsEqual<@P.AsInt>>
.View.Select(Base, accessBranch.BranchID);

if (currentBranch == null)
{
throw new PXException("Unable to retrieve the Branch record.");
}

int? bAccountID = null;

if (currentBranch != null)
{
// First, try the organization's BAccountID
PX.Objects.GL.DAC.Organization organization = SelectFrom<PX.Objects.GL.DAC.Organization>
.Where<PX.Objects.GL.DAC.Organization.organizationID
.IsEqual<@P.AsInt>>
.View.Select(
Base,
currentBranch.OrganizationID);

bAccountID = organization?.BAccountID
?? currentBranch.BAccountID;
}

if (bAccountID == null)
{
throw new PXException(
"Unable to determine the current organization's BAccountID.");
}

PXTrace.WriteInformation(
$"Current BranchID: {currentBranch?.BranchID}, " +
$"OrganizationID: {currentBranch?.OrganizationID}, " +
$"BAccountID: {bAccountID}");

// Display the result in the Acumatica trace
throw new PXException(
$"Retrieved BAccountID: {bAccountID}");
}
#endregion

 

 

Hope this helps!

Translate