Skip to main content

Notifications

Announcements

No record found.

Supply chain | Supply Chain Management, Commerce
Answered

I want to post invoice only whatever dispatch today

(5) ShareShare
ReportReport
Posted on by 1,993
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.");
        }
    }

}
 
  • Suggested answer
    P Jackson Profile Picture
    P Jackson 1,588 on at
    I want to post invoice only whatever dispatch today
    Use the 'Select' option:

  • Verified answer
    faiz7049 Profile Picture
    faiz7049 1,993 on at
    I want to post invoice only whatever dispatch today
    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.");
        }
     
  • faiz7049 Profile Picture
    faiz7049 1,993 on at
    I want to post invoice only whatever dispatch today
    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
  • faiz7049 Profile Picture
    faiz7049 1,993 on at
    I want to post invoice only whatever dispatch today
    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
    faiz7049 1,993 on at
    I want to post invoice only whatever dispatch today
    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
  • Suggested answer
    P Jackson Profile Picture
    P Jackson 1,588 on at
    I want to post invoice only whatever dispatch today
    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
    faiz7049 1,993 on at
    I want to post invoice only whatever dispatch today
    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
  • faiz7049 Profile Picture
    faiz7049 1,993 on at
    I want to post invoice only whatever dispatch today
    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
     
  • Suggested answer
    Aymen CHELBI Profile Picture
    Aymen CHELBI 183 on at
    I want to post invoice only whatever dispatch today
    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
    faiz7049 1,993 on at
    I want to post invoice only whatever dispatch today
    Hi Experts,
     
    I debug the code and getting sales order Id based on delivery date but invoices not posted.
     
    Thanks,
    Faiz

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Congratulations to the January Top 10 leaders!

Check out the January community rock stars...

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,286 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,064 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans