Acumatica分支扩展问题:默认分支位置字段值批量保存至所有分支
Got it, let's break down why your custom default location field is getting saved to every Branch record instead of just the selected one, and how to fix it. This is a common hiccup when extending Acumatica DACs, so let's walk through the solutions step by step.
First, Verify Your DAC Extension Setup
The most common culprit here is either a misconfigured DAC extension or an issue with how the field is scoped to individual branches. Let's start with the correct structure for your extension:
[PXTable(typeof(Branch.branchID), IsOptional = false)] public class BranchExtension : PXCacheExtension<Branch>, IBqlTable { #region UsrDefaultBranchLocationID [PXDBInt] [PXUIField(DisplayName = "Default Branch Location")] // Critical: Filter selector to ONLY show locations for the current branch [PXSelector( typeof(Search<Location.locationID, Where<Location.branchID, Equal<Current<Branch.branchID>>>>), typeof(Location.locationCD), typeof(Location.descr), SubstituteKey = typeof(Location.locationCD), DescriptionField = typeof(Location.descr))] public virtual int? UsrDefaultBranchLocationID { get; set; } public abstract class usrDefaultBranchLocationID : PX.Data.BQL.BqlInt.Field<usrDefaultBranchLocationID> { } #endregion }
Key checks here:
- The
[PXTable(typeof(Branch.branchID))]attribute ensures each extension record is strictly linked to a single Branch via its ID. - The selector uses
Current<Branch.branchID>to lock down location options to only the branch you're editing—this not only prevents invalid selections but also reinforces that the field is tied to the current record.
Check the UI Field Binding
Next, confirm your field is correctly wired in the Branch maintenance page (BR102000):
- Open the Layout Editor, select your custom field.
- Under the Data Field property, make sure it's set to
BranchExtension.UsrDefaultBranchLocationID(not a generic or global field). - Ensure the field lives in a container that uses the default
Branchdata view (this is usually the case, but it's worth double-checking to avoid cross-view binding issues).
Look for Custom Event Logic That's Updating All Branches
If your DAC extension looks solid, the problem might be in custom event handlers (like RowPersisting, RowUpdated) in your Branch graph.
Avoid this mistake: If you're looping through all Branch records and setting the field, that's why every branch gets updated. For example:
// ❌ WRONG: This overwrites the field for every branch in the system protected virtual void Branch_RowPersisting(PXCache cache, PXRowPersistingEventArgs e) { foreach (Branch b in Base.Branch.Select()) { BranchExtension ext = cache.GetExtension<BranchExtension>(b); ext.UsrDefaultBranchLocationID = someValue; } }
Do this instead: Only modify the extension for the current branch being saved:
// ✅ CORRECT: Only updates the branch you're actively editing protected virtual void Branch_RowPersisting(PXCache cache, PXRowPersistingEventArgs e) { Branch currentBranch = e.Row as Branch; if (currentBranch == null) return; BranchExtension ext = cache.GetExtension<BranchExtension>(currentBranch); // Add logic here only for the current branch (e.g., set a default if empty) }
Verify Database Table Structure
Finally, check the underlying database table for your extension (it'll be named something like BranchExtension or usr_BranchExtension). Ensure there's a BranchID column that links each extension record to a single Branch. If this column is missing or not set as a foreign key, Acumatica can't associate the field value with the correct branch.
If you've followed these steps, your default location field should now save only to the branch you're editing, not all branches.
内容的提问来源于stack exchange,提问作者yc9nyw08vf

