web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

Creating Accounting Distributions for Free Text Invoice via X++

(5) ShareShare
ReportReport
Posted on by 10

Has anyone ever created accounting distributions for a Free Text Invoice using X++ code?

 

I'm currently trying to generate the distribution records programmatically, and while the records appear to be created, the first time a user opens the Distribute Amount form, the system always recreates the records and overrides what I generated.

 

Is there a proper way to create and finalize the accounting distributions in X++ so that they persist and are not regenerated on form load?

I have the same question (0)
  • Suggested answer
    Holly Huffman Profile Picture
    6,538 Super User 2025 Season 2 on at
     
    Good morning, afternoon, or evening depending on your location!
     
    Below is an approach that many have found useful when dealing with this classic issue in free text invoice accounting distributions.
     
    The Core Issue
    When you generate accounting distributions programmatically (via X++), the system’s built‐in “Distribute Amount” form re-runs its logic when it loads. That means it re-imports or regenerates the default distributions based on header/line data—overwriting your custom entries. In effect, unless you “finalize” or flag your distributions as complete, the form thinks nothing has been done and thus rebuilds everything.
     
    The Proper Approach
    To have your custom accounting distributions persist, you’ll need to work WITH the distribution framework rather than bypassing it. Consider these guidelines:
    1. Leverage Built-In Methods and Business Logic
      Instead of manually inserting records, call the same X++ classes/methods that the “Distribute Amount” button uses.
      • For example, methods in a class like DistributeAmounts (or similar, based on your release) help you compute and then finalize distributions.
      • These methods often include logic that sets flags (or statuses) on the header or line records once distributions have been produced.
      • By calling these methods (or following their pattern), you can allow your custom modifications to be integrated into the final distribution set.
    2. Mark Your Distributions as Finalized
      One common pattern is to update a status field on the free text invoice header (or on the distribution records) that signals distributions are “finalized” or “submitted.”
      • For example, if your header or line has a field such as AccountingDistributionStatus, you must set it (for instance, to a value like Finalized) after you create your custom distributions.
      • This finalization tells the system “don’t recalc my distributions” when the form loads.
    3. Prevent Regeneration on Form Load
      You might also consider overriding (or subscribing to) the event/method that fires during the “Distribute Amount” form’s init routine.
      • Here, check if your distribution records already exist in a finalized state, and if so, bypass the regeneration logic.
      • This might mean writing a small event handler in X++ that suppresses the automatic rebuild if your custom flag is detected.
     
    A Pseudo-Code Example
    Below is a simplified example to illustrate the pattern:
    // Retrieve the free text invoice header and associated lines
    FreeTextInvoiceHeader   invoiceHeader = ...;
    FreeTextInvoiceLine     invoiceLine   = ...;

    // Compute the default distributions using the supported method (adjust per your version)
    AccountingDistribution[] distributions = DistributeAmounts::computeDistributions(invoiceHeader, invoiceLine);

    // Loop through the distributions and apply custom modifications
    for (int i = 0; i < distributions.length(); i++)
    {
        // Example: update distribution percentage, main account, or financial dimensions
        distributions[i].MainAccount    = 'XXXXXX';
        distributions[i].DistributionPct = 50;
        // ... any other custom logic
    }

    // **Important:** Mark the invoice as having its distributions finalized to prevent re-generation
    invoiceHeader.AccountingDistributionStatus = AccountingDistributionStatus::Finalized;
    invoiceHeader.doUpdate();

    // Optionally, call a method to "finalize" the distributions
    // (this can also commit the custom distributions to the ledger interface)
    DistributeAmounts::finalizeDistributions(invoiceHeader);

    Note:
    The actual class/method names and status field values may differ based on your D365FO version and configuration. Always refer to your version-specific documentation and test thoroughly in a non-production environment.
    Summary
    • Use the supported X++ methods: Call the business logic that Powers the “Distribute Amount” form.
    • Finalize your distributions: Set a status (or a similar flag) so that the system won’t auto-regenerate them.
    • Control form behavior: If necessary, override the form’s event logic so it respects your custom distribution records.
    By following these guidelines, your accounting distributions should persist as intended after you create them via X++.
     
    Please note: I teamed up with CoPilot AI to research and craft the best response to your question!
    Hope this helps some!
  • Jonas "Jones" Melgaard Profile Picture
    4,988 Most Valuable Professional on at
     
    The whole Source Document framework is a bit tricky and it's VERY easy to mess data up. And support is not keen on fixing GL issues caused by customizations.
     
    That said, have a look at CustPostInvoice.processSourceDocument. It's responsible for posting of customer invoices and part of the posting process is generating and finalizing the distribution.
     
     
    What is it you want to archive? Do you want to change the accounting of the distribution?
     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 456 Super User 2025 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 429 Most Valuable Professional

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 239 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans