You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

创建Journal Entry时切换公司与分支异常,求正确实现方案

Troubleshooting Branch/Company Switching for Journal Entries

Alright, let’s tackle this step by step—since you’re working with journal entries and branch/company switching, I’m assuming we’re in the Dynamics 365 Finance & Operations space here.

First, let’s address your core question: Is the branch switching code you’re using the best approach? The short answer is that the standard, recommended method from Microsoft uses global context switching functions, but let’s break down how to validate what’s going wrong and confirm the right approach.

1. Standard Branch/Company Switching Method

The official, most reliable way to switch company and branch before creating records is using Global::changeCompany() and Global::changeBranch()—these directly update the current session’s context, which should be respected when inserting new records like journal entries. Here’s a clean example of how this should work:

// First switch to the target company (ensure it exists in your environment)
Global::changeCompany('YourTargetCompany');

// Then switch to the corresponding branch (must belong to the target company!)
Global::changeBranch('YourTargetBranch');

// Now create your journal entry in this updated context
ttsBegin;
JournalTable journalTable;
journalTable.JournalNum = JournalTable::nextNum('GenJrn');
journalTable.JournalType = JournalType::Daily;
journalTable.DataAreaId = curext(); // Should match your target company
journalTable.BranchId = Global::currentBranch(); // Should match your target branch
// Add other required fields (like Description, LedgerDimension, etc.)
journalTable.insert();
ttsCommit;

2. Why AccessInfo Might Be Giving Wrong Results

If you’re using AccessInfo to check the current branch, that could be the root of your confusion. AccessInfo often relies on cached data or custom environment-specific logic instead of directly reading the active session context. A far more reliable way to verify the current branch/company is to use the Session class, which pulls directly from the active session:

// Immediately after switching, validate the context
info(strFmt("Current Company: %1, Current Branch: %2", Session::currentCompany(), Session::currentBranch()));

If this returns the correct values but AccessInfo doesn’t, then the issue is with AccessInfo (not your switching code). If this returns the wrong values, then your switching code isn’t working as expected.

3. Troubleshooting Ruslan’s Code

If you’re using a custom method from Ruslan, double-check that it’s actually leveraging the standard context switching functions. Common pitfalls here include:

  • Forgetting branch-company dependency: You must switch the company first, then switch to a branch that’s linked to that company—switching branches before companies can cause silent failures.
  • Calling switches inside a transaction: Context changes can behave unpredictably inside ttsBegin/ttsCommit blocks. Always switch before starting the transaction.
  • Missing validation: If the target branch/company doesn’t exist, the switch method might fail silently. Add checks like if (!BranchInfo::exists('YourTargetBranch')) to validate before switching.

4. Best Practices for Reliable Switching

  • Always validate the context immediately after switching using Session::currentCompany() and Session::currentBranch()—don’t rely on custom info classes like AccessInfo for debugging.
  • If you’re creating records across multiple companies/branches, reset the context back to the original company/branch after your operation to avoid unintended side effects for subsequent code.
  • For reading data across companies, use CrossCompany queries, but for writing records, explicit context switching is the recommended approach.

内容的提问来源于stack exchange,提问作者pmfith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:24:42