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

Community site session details

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

Error when posting Invoices with code in Sandbox/Production environments but not in dev machines

(4) ShareShare
ReportReport
Posted on by 104
I have a solution about posting Sales Order Invoice which works for a year in Production and Sandbox environments and all of the sudden it throws the error "A number sequence has not been set up for the reference Customer invoice in the area Sales" (or "Sales Credit Note" and area "Sales" depending on the type of Invoice). I can't replicate the error in my dev environment or any other.
This situation had occurred one time in the past again in Sandbox and when Production database(it worked fine then) took the place of Sandbox database, the error went away.
I use the SalesFormLetter class like that:
 
SalesFormLetter salesFormLetter = SalesFormLetter::construct(DocumentStatus::Invoice);
SalesParmLine parmLine;
SalesParmUpdate salesParmUpdate;
//A SalesTable that already exists with one line unposted
salesFormLetter.salesTable(initialSalesTable); 
salesFormLetter.transDate(invoiceDate);
salesFormLetter.specQty(SalesUpdate::All);
salesFormLetter.proforma(false);
salesFormLetter.printFormLetter(false);
salesParmUpdate = salesFormLetter.salesParmUpdate();
//Passing a NumberSequenceGroup string to a Custom field in order to allocate Invoice and Voucher num from this later
salesParmUpdate.CustomNumberSequenceGroupId = numberSequenceGroup;
salesFormLetter.createParmUpdateFromParmUpdateRecord(salesParmUpdate);
salesFormLetter.initParmSalesTable(initialSalesTable);
salesFormLetter.initParameters(salesParmUpdate, Printout::After);
salesFormLetter.initLinesQuery();

//Remove lines of Sales Order that don't belong to this process
while select parmLine where parmLine.ParmId == salesFormLetter.parmId()
{
    if(!someMap.exists(parmLine.SalesLineRecId))
    {
        parmLine.selectForUpdate(true);
        parmLine.delete();
    }
}

salesFormLetter.run();
 
The code will eventually reach the SalesInvoiceJournalCreateBase.allocateNumAndVoucher() method which will get the Invoice and Voucher number sequences from custom Number Sequence Group of SalesParmUpdate above.
I can't debug in these environments so my guess is that NumberSequenceGroup is empty at that point so it searches about the main parameters in "Accounts Receivable Parameters > Number Sequences" and throws error because they are not set.
If I put number sequences there, then posting happens correctly by ignoring the Number Sequence Group setup.
If I use the original Generate Invoice button by selecting that Number Sequence Group in the proforma custom control it works too.
My first thought is that something is wrong with the db data so I tried to clear the Parm tables data with "Clean up sales update history" job from Sales and Marketing module but didn't work.
Any thoughts or guesses would be appreciated.
Categories:
I have the same question (0)
  • Vasileios Papoglou Profile Picture
    104 on at
    Error when posting Invoices with code in Sandbox/Production environments but not in dev machines
    Hello all, I found out that the problem is that SalesFormLetter::construct() does not create a SalesParmUpdate record so in the contract that it creates the parmUpdate is empty. Any ideas why that happens suddenly? 
  • Suggested answer
    Saalim Ansari Profile Picture
    648 on at
    Error when posting Invoices with code in Sandbox/Production environments but not in dev machines

    Hi @Vasileios,

    Thanks for sharing the detailed context — really helpful in understanding the issue.

    Since the same logic works when triggered from the UI but fails in code, and given your suspicion about SalesParmUpdate inconsistencies, a few things to consider:

    Double-check when you assign CustomNumberSequenceGroupId — try moving that assignment after initParameters() and just before run() to ensure it doesn’t get overwritten or ignored during parameter initialization.
     
    Validate runtime data — just before calling run(), add a quick info() or debug log for salesFormLetter.salesParmUpdate().CustomNumberSequenceGroupId to confirm the value is still correct at execution time.
     
    Possible stale data or incomplete cleanup — consider doing a full cleanup of SalesParmUpdate, SalesParmLine, and related tables manually if you suspect corruption that the standard cleanup job didn’t catch.
     
    Dev vs Sandbox/Prod environment differences — dev environments can be more forgiving with setups, so I agree, using a production DB copy in a sandbox is a good move.
     

    Also, I highly recommend checking out this article by Gaurav Gulati:
    🔗 How to debug sandbox/UAT environment in D365FO

    It walks you through how to attach a debugger to a sandbox/UAT environment — super helpful in scenarios like this when code behaves differently in non-dev tiers.

    Let us know how it goes once you try these steps.

  • Vasileios Papoglou Profile Picture
    104 on at
    Error when posting Invoices with code in Sandbox/Production environments but not in dev machines
    Hello André and thanks for your response. No, the number sequence is fine and the setup is the same as it was while it worked fine. Besides, when I choose the same number sequence group on the same custom field but by using the proforma of Generate Invoice it works fine. The error occurs only when I post Invoice with code despite the fact that I use the same input on the same custom field and that it worked until recently. Taking a copy of the database may be the only way to investigate it further. One thing that I have noticed in the past is that SalesParmUpdate table has corrupted records some times that make the code crash until I clean it. So I am afraid that something similar happens now but I haven't figured out yet.
  • Suggested answer
    André Arnaud de Calavon Profile Picture
    298,959 Super User 2025 Season 2 on at
    Error when posting Invoices with code in Sandbox/Production environments but not in dev machines
    Hi Vasileios,
     
    Maybe the number sequence was at the end of its sequence? Have you verified the settings. I would suggest debugging using a copy of the production database.
    You can perform debugging in a non-production environment. Read the next article how to connect the sandbox for performing debugging here: Debug a copy of the production database - Finance & Operations | Dynamics 365 | Microsoft Learn

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

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

#1
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 2,239

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 604 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans