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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Supply chain | Supply Chain Management, Commerce
Answered

I want to post invoice only whatever dispatch today

(6) ShareShare
ReportReport
Posted on by 2,382
Hi Experts,
 
Customer requirement is to post invoice whatever dispatch today only consider Order Type = Sales order
 
Below is incomplete code. Could anyone please me on this.
final class SFA_PostInvoicesForTodayShipDate
{
    public static void main(Args _args)
    {
        SFA_PostInvoicesForTodayShipDate::run();
    }

    public static void run()
    {
        SalesTable              salesTable;
        SalesParmUpdate         salesParmUpdate;
        SalesFormLetter         salesFormLetter;
        SalesParmLine           salesParmLine;
        SalesLine               salesLine;
        SalesParmSubLine        salesParmSubLine;
        SalesParmTable          salesParmTable;
        SalesId                 salesId;
        CustPackingSlipJour     custPackingSlipJour; // Packing slip journal
        CustPackingSlipTrans    custPackingSlipTrans; // Packing slip transactions
        TransDate               today = today();
        boolean                 invoicePosted;

        // Select packing slips with delivery date = today
        while select custPackingSlipJour
            where custPackingSlipJour.DeliveryDate == today // Use DeliveryDate
            join custPackingSlipTrans
            where custPackingSlipTrans.PackingSlipId == custPackingSlipJour.PackingSlipId
            join salesLine
            where salesLine.InventTransId == custPackingSlipTrans.InventTransId
            join salesTable
            where salesTable.SalesId == salesLine.SalesId
               && salesTable.SalesType == SalesType::Sales // Filter for Sales Orders
        {
            salesId = salesTable.SalesId;

            // Initialize the sales form letter for invoice posting
            salesFormLetter = SalesFormLetter::construct(DocumentStatus::Invoice);

      
            salesFormLetter.initParmSalesTable(salesTable);
            salesParmUpdate = salesFormLetter.salesParmUpdate();

            
           
            salesFormLetter.salesParmUpdate(salesParmUpdate);
           
            
            salesParmTable.initFromSalesTable(salesTable);

            //Initialize the sales parm line
            salesParmLine.initFromSalesLine(salesLine);
            salesParmLine.ParmId = salesParmTable.ParmId;

            // Post the invoice
            try
            {
                salesFormLetter.run();
                info(strFmt("Invoice posted for Sales Order: %1", salesId));
                invoicePosted = true;
            }
            catch (Exception::Error)
            {
                error(strFmt("Error posting invoice for Sales Order: %1", salesId));
                invoicePosted = false;
            }
        }

        if (!invoicePosted)
        {
            warning("No invoices were posted.");
        }
    }

}
 
I have the same question (0)
  • faiz7049 Profile Picture
    2,382 on at
    Hi Experts,
     
    I debug the code and getting sales order Id based on delivery date but invoices not posted.
     
    Thanks,
    Faiz
  • Suggested answer
    Aymen CHELBI Profile Picture
    578 on at
    Hello,
     
    I suggest you test this version : 
     
    final class SFA_PostInvoicesForTodayShipDate
    {
        public static void main(Args _args)
        {
            SFA_PostInvoicesForTodayShipDate::run();
        }
        public static void run()
        {
            SalesTable              salesTable;
            SalesFormLetter         salesFormLetter;
            SalesLine               salesLine;
            CustPackingSlipJour     custPackingSlipJour; // Packing slip journal
            CustPackingSlipTrans    custPackingSlipTrans; // Packing slip transactions
            TransDate               today = today();
            boolean                 invoicePosted = false;
            // Select packing slips with delivery date = today and Sales order type
            while select custPackingSlipJour
                where custPackingSlipJour.DeliveryDate == today // Use DeliveryDate
                join custPackingSlipTrans
                where custPackingSlipTrans.PackingSlipId == custPackingSlipJour.PackingSlipId
                join salesLine
                where salesLine.InventTransId == custPackingSlipTrans.InventTransId
                join salesTable
                where salesTable.SalesId == salesLine.SalesId
                   && salesTable.SalesType == SalesType::Sales // Filter for Sales Orders
            {
                try
                {
                    // Initialize the sales form letter for invoice posting
                    salesFormLetter = SalesFormLetter::construct(DocumentStatus::Invoice);
                    // Initialize the sales form letter with the sales order
                    salesFormLetter.initParmSalesTable(salesTable);
                    // Post the invoice
                    salesFormLetter.run();
                    // If invoice posted successfully, log it
                    info(strFmt("Invoice posted for Sales Order: %1", salesTable.SalesId));
                    invoicePosted = true;
                }
                catch (Exception::Error)
                {
                    // Handle any error during posting
                    error(strFmt("Error posting invoice for Sales Order: %1", salesTable.SalesId));
                }
            }
            // If no invoices were posted, give a warning
            if (!invoicePosted)
            {
                warning("No invoices were posted.");
            }
        }
    }
     
     
    Best regards,
  • faiz7049 Profile Picture
    2,382 on at
    Hi Aymen,
     
    Thank you for your code. In debugging, getting below information. Still invoice is not posted.

     
      Name Value Type
    salesLine Cannot evaluate expression since the function evaluation requires all threads to run. Dynamics.AX.Application.SalesLine
     
  • faiz7049 Profile Picture
    2,382 on at
    Hi Experts,
     
    I have updated the below code now only check packingslip which is not invoiced but still not updating invoices.
     
    final class SFA_PostInvoicesForTodayShipDate
    {
        public static void main(Args _args)
        {
            SFA_PostInvoicesForTodayShipDate::run();
        }
    
        public static void run()
        {
            SalesTable              salesTable;
            SalesParmUpdate         salesParmUpdate;
            SalesFormLetter         salesFormLetter;
            SalesParmLine           salesParmLine;
            SalesLine               salesLine;
            SalesParmSubLine        salesParmSubLine;
            SalesParmTable          salesParmTable;
            SalesId                 salesId;
            InventTrans             inventTrans;
            CustPackingSlipJour     custPackingSlipJour; // Packing slip journal
            CustPackingSlipTrans    custPackingSlipTrans; // Packing slip transactions
            TransDate               today = str2Date("19/12/2024",123);//today();
            boolean                 invoicePosted;
    
            // Select packing slips with delivery date = today
            while select custPackingSlipJour
                where custPackingSlipJour.DeliveryDate == today // Use DeliveryDate
              exists  join inventTrans
             where inventTrans.InvoiceId=="" && inventTrans.PackingSlipId == custPackingSlipJour.PackingSlipId
                join custPackingSlipTrans
                where custPackingSlipTrans.PackingSlipId == custPackingSlipJour.PackingSlipId
                join salesLine
                where salesLine.InventTransId == custPackingSlipTrans.InventTransId
                join salesTable
                where salesTable.SalesId == salesLine.SalesId
                   && salesTable.SalesType == SalesType::Sales // Filter for Sales Orders
            {
                try
                {
                    // Initialize the sales form letter for invoice posting
                    salesFormLetter = SalesFormLetter::construct(DocumentStatus::Invoice);
                    // Initialize the sales form letter with the sales order
                    salesFormLetter.initParmSalesTable(salesTable);
                    // Post the invoice
                    salesFormLetter.run();
                    // If invoice posted successfully, log it
                    info(strFmt("Invoice posted for Sales Order: %1", salesTable.SalesId));
                    invoicePosted = true;
                }
                catch (Exception::Error)
                {
                    // Handle any error during posting
                    error(strFmt("Error posting invoice for Sales Order: %1", salesTable.SalesId));
                }
            }
    
            if (!invoicePosted)
            {
                warning("No invoices were posted.");
            }
        }
    
    }
    Thank you,
    Faiz
  • Suggested answer
    P Jackson Profile Picture
    1,614 on at
    There's a standard Batch job for sales invoice posting and you could use the parameter for 'Quantity' [Delivery note]. You could also use the 'Select' criteria for the order type, if necessary.
  • faiz7049 Profile Picture
    2,382 on at
    Hi Jackson,
     
    Thank for reply, How can apply filter on Invoice Batch process . Requirement is to post invoices against all PackingSlip whatever dispatch today and consider Order Type “Sales Order”. I want to use standard one but I cannot filter on Batch.
     
    Thanks,
    Faiz
  • faiz7049 Profile Picture
    2,382 on at
    Hi Experts,
     
    I have update my code and now invoice is generating. Problem is it's also including return order. But I filter salesTable.SalesType == SalesType::Sales.
     
    Could you please help me. I do not want to post return order.
     
    final class SFA_PostInvoicesForTodayShipDate
    {
        public static void main(Args _args)
        {
            SFA_PostInvoicesForTodayShipDate::run();
        }
    
        public static void run()
        {
            SalesTable              salesTable;
            SalesParmUpdate         salesParmUpdate;
            SalesFormLetter         salesFormLetter;
            SalesParmLine           salesParmLine;
            SalesLine               salesLine;
            SalesParmSubLine        salesParmSubLine;
            SalesParmTable          salesParmTable;
            SalesId                 salesId;
            InventTrans             inventTrans;
            CustPackingSlipJour     custPackingSlipJour; // Packing slip journal
            CustPackingSlipTrans    custPackingSlipTrans; // Packing slip transactions
            TransDate               today = str2Date("19/12/2024",123);//today();
            boolean                 invoicePosted;
    
            // Select packing slips with delivery date = today
            while select custPackingSlipJour
                //where custPackingSlipJour.DeliveryDate == today // Use DeliveryDate
              exists  join inventTrans
             where inventTrans.InvoiceId=="" && inventTrans.PackingSlipId == custPackingSlipJour.PackingSlipId
                join custPackingSlipTrans
                where custPackingSlipTrans.PackingSlipId == custPackingSlipJour.PackingSlipId            
                join salesLine
                where salesLine.InventTransId == custPackingSlipTrans.InventTransId
                join salesTable
                where salesTable.SalesId == salesLine.SalesId
                   && salesTable.SalesType == SalesType::Sales
                   
            {
                try
                {
                    // Initialize the sales form letter for invoice posting
                    salesFormLetter = SalesFormLetter::construct(DocumentStatus::Invoice);
                    // Initialize the sales form letter with the sales order
                    salesFormLetter.initParmSalesTable(salesTable);
                    // Post the invoice
                    salesFormLetter.update(salesTable,systemDateGet(),SalesUpdate::PackingSlip);
                    // If invoice posted successfully, log it
                    info(strFmt("Invoice posted for Sales Order: %1", salesTable.SalesId));
                    invoicePosted = true;
                }
                catch (Exception::Error)
                {
                    // Handle any error during posting
                    error(strFmt("Error posting invoice for Sales Order: %1", salesTable.SalesId));
                }
            }
    
            if (!invoicePosted)
            {
                warning("No invoices were posted.");
            }
        }
    
    }
    Thanks,
     
    Faiz
  • faiz7049 Profile Picture
    2,382 on at
    Hi Experts,
     
    When I use salesFormLetter.update() method invoice is posting but while loop filter is not working
    salesFormLetter.update(salesTable,systemDateGet(),SalesUpdate::PackingSlip);
    If I use salesFormLetter.run() method while filter is working but no invoice posting.

    Please help me.
     
    Thanks,
     
    Faiz
  • Verified answer
    faiz7049 Profile Picture
    2,382 on at
    Hi Experts,
     
    Now codes are working.
     
        while select salesTable
         where salesTable.SalesType == SalesType::Sales
            exists join custPackingSlipJour
           where custPackingSlipJour.DeliveryDate == today && custPackingSlipJour.SalesId == salesTable.SalesId            
          
    
        {
            try
            {
                // Initialize the sales form letter for invoice posting
                salesFormLetter = SalesFormLetter::construct(DocumentStatus::Invoice);
                // Initialize the sales form letter with the sales order
                salesFormLetter.initParmSalesTable(salesTable);
                // Post the invoice
    
    
                salesFormLetter.update(salesTable,systemDateGet(),SalesUpdate::PackingSlip);
               
                // If invoice posted successfully, log it
                info(strFmt("Invoice posted for Sales Order: %1", salesTable.SalesId));
                invoicePosted = true;
            }
            catch (Exception::Error)
            {
                // Handle any error during posting
                error(strFmt("Error posting invoice for Sales Order: %1", salesTable.SalesId));
            }
        }
    
        if (!invoicePosted)
        {
            warning("No invoices were posted.");
        }
     
  • Suggested answer
    P Jackson Profile Picture
    1,614 on at
    Use the 'Select' option:

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

News and Announcements

Season of Giving Solutions is Here!

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 > Supply chain | Supply Chain Management, Commerce

#1
Laurens vd Tang Profile Picture

Laurens vd Tang 288 Super User 2025 Season 2

#2
André Arnaud de Calavon Profile Picture

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

#3
Sumit Singh Profile Picture

Sumit Singh 46

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans