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,