So here is another code snippet, that I develop after two hours of testing and understanding the D365 F&O that get all non invoiced , delivery notes or packingslip. I want to restrict the code to invoice against Delivery note, If no non posted delivery note exists, Code will not generate the invoice.
public static str getUninvoicedPackingSlipsBySalesId(SalesId _salesId)
{
CustPackingSlipJour custPackingSlipJour; // Packing slip journal
InventTrans inventTrans; // Inventory transaction
str packingSlipIds = "";
container distinctPackingSlips; // Container to store unique Packing Slip IDs
int i = 0;
// Fetch only un-invoiced Packing Slips based on Sales ID
while select inventTrans
where inventTrans.InvoiceId == ""
join custPackingSlipJour
where custPackingSlipJour.PackingSlipId == inventTrans.PackingSlipId
&& custPackingSlipJour.SalesId == _salesId
{
// Check if the Packing Slip ID is already in the container
if (conFind(distinctPackingSlips, custPackingSlipJour.PackingSlipId) == 0)
{
distinctPackingSlips += custPackingSlipJour.PackingSlipId; // Add to container if unique
}
}
return con2Str(distinctPackingSlips); // Return the comma-separated string or empty if none found
}