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, ...
Answered

X++ Post Sales Invoice with Manual Assigned Invoice ID

(0) ShareShare
ReportReport
Posted on by 1,214

Hi,

I'm referring this code to post sales order invoice with X++.

Is it possible to assign the Invoice ID in code instead of relying on the system generated number sequence? Just like PO invoice.

Thank you.

I have the same question (0)
  • Verified answer
    Sergei Minozhenko Profile Picture
    23,093 on at

    Hi Hairul Hazri,

    You can try another approach described in the article gearupdax.blogspot.com/.../x-code-to-post-sales-order-invoice-line.html and pass the invoice number to SalesParmTable.Num_W field. Note, that field has restrictions by country regions and you may need to extend the list of codes for it.

  • HAIRUL HAZRI Profile Picture
    1,214 on at

    Thank you Sergei for the direction. Here's my final codes:

    static void post(salesTable _salesTable)
    {
        salesFormLetter         salesFormLetter;
        salesFormletterParmData salesFormLetterParmData;
        salesParmUpdate         salesParmUpdate;
        salesParmTable          salesParmTable;
        salesParmLine           salesParmLine;
        salesTable              salesTable;
        SalesLine               salesLine;
        CustInvoiceJour custInvoiceJour;
    
        ttsbegin;
        salesTable  = _salesTable;
        salesFormLetterParmData = salesFormletterParmData::newData(DocumentStatus::Invoice, VersioningUpdateType::Initial);
    
        salesFormLetterParmData.parmOnlyCreateParmUpdate(true);
        salesFormLetterParmData.createData(false);
        salesParmUpdate = salesFormLetterParmData.parmParmUpdate();
    
        salesParmTable.clear();
        salesParmTable.TransDate                = salesTable.InvoiceDateTmp;    //custom InvoiceDate field
        salesParmTable.Num_W                    = salesTable.InvoiceIdTmp;      //custom InvoiceId field
        salesParmTable.Ordering                 = DocumentStatus::Invoice;
        salesParmTable.ParmJobStatus            = ParmJobStatus::Waiting;
        salesParmTable.salesId                  = salesTable.salesId;
    
        salesParmTable.salesName                = salesTable.salesName;
        salesParmTable.DeliveryName             = salesTable.DeliveryName;
        salesParmTable.DeliveryPostalAddress    = salesTable.DeliveryPostalAddress;
        salesParmTable.CustAccount              = salesTable.CustAccount;
        salesParmTable.CurrencyCode             = salesTable.CurrencyCode;
        salesParmTable.InvoiceAccount           = salesTable.InvoiceAccount;
        salesParmTable.ParmId                   = salesParmUpdate.ParmId;
        salesParmTable.insert();
    
        while select salesLine where salesLine.SalesId == salesTable.SalesId
        {
            salesParmLine.InitFromsalesLine(salesLine);
            salesParmLine.DeliverNow    = salesLine.SalesQty;
            salesParmLine.ParmId        = salesParmTable.ParmId;
            salesParmLine.TableRefId    = salesParmTable.TableRefId;
            salesParmLine.setQty(DocumentStatus::Invoice, false, true);
            salesParmLine.setLineAmount(salesLine);
            salesParmLine.insert();
        }
        ttscommit;
        salesFormLetter = salesFormLetter::construct(DocumentStatus::Invoice);
        salesFormLetter.transDate(_transDate);
        salesFormLetter.proforma(false);
        salesFormLetter.specQty(salesUpdate::All);
        salesFormLetter.salesTable(salesTable);
        salesFormLetter.parmId(salesParmTable.ParmId);
        salesFormLetter.salesParmUpdate(salesFormLetterParmData.parmParmUpdate());
    
        //validate Num_W
        if(salesFormLetter.checkDuplicateNum_W(salesParmTable.Num_W, salesParmTable.SalesId, salesParmTable.Transdate))
        {
            salesFormLetter.run();
    
            if (salesFormLetter.parmJournalRecord().TableId == tableNum(CustInvoiceJour))
            {
                custInvoiceJour = salesFormLetter.parmJournalRecord();
                    
                info(strFmt('Sales Order #:%1 has been successfully posted and Invoice #:%2 is created!',
                        custInvoiceJour.SalesId, custInvoiceJour.InvoiceId));
            }
        }
        else
            Error('Invoice ID invalid.');
    }

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
Martin Dráb Profile Picture

Martin Dráb 559 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 250 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans