
Hi,
You’re on the right track, but I wouldn’t try to force the freight line into the normal purchase-line/drop-shipment linkage. The freight charge does not belong on the purchase order, so it should remain an independent sales line.
For this scenario, the most appropriate subscriber is OnAfterPostPurchaseDocDropShipment in codeunit 90 "Purch.-Post". It runs after Business Central has created the drop-shipment sales shipment and provides the resulting sales shipment number.
From that event you can:
"Sales Shipment Header" using SalesShptNo."Order No." to locate the original sales order."Quantity Shipped" and the related outstanding quantities on the original sales line.For example:
[EventSubscriber(
ObjectType::Codeunit,
Codeunit::"Purch.-Post",
'OnAfterPostPurchaseDocDropShipment',
'',
false,
false)]
local procedure OnAfterPostPurchaseDocDropShipment(
SalesShptNo: Code[20];
CommitIsSuppressed: Boolean;
PreviewMode: Boolean)
var
SalesShptHeader: Record "Sales Shipment Header";
SalesLine: Record "Sales Line";
begin
if PreviewMode then
exit;
if SalesShptNo = '' then
exit;
if not SalesShptHeader.Get(SalesShptNo) then
exit;
SalesLine.SetRange("Document Type", SalesLine."Document Type"::Order);
SalesLine.SetRange("Document No.", SalesShptHeader."Order No.");
SalesLine.SetRange(Type, SalesLine.Type::Item);
SalesLine.SetRange("Gen. Prod. Posting Group", 'FREIGHT');
SalesLine.SetFilter("Outstanding Quantity", '<>0');
if SalesLine.FindSet(true) then
repeat
PostFreightShipmentLine(SalesShptHeader, SalesLine);
until SalesLine.Next() = 0;
end;
PostFreightShipmentLine should create the "Sales Shipment Line" and update the source sales line in the same way that "Sales-Post" handles a non-inventory item. Be careful not to update only "Quantity Shipped" manually; you also need the base quantity, outstanding quantities, "Qty. to Ship", and potentially blanket-order information. Reusing or closely following the standard Sales-Post routines is safer than reproducing only some of the field updates.
I would not call "Sales-Post" directly from inside "Purch.-Post". That can result in nested posting, commits, locking, number-series issues, and duplicate shipments.
Also, avoid using OnAfterSalesShptLineInsert for the whole operation. It fires once for every linked drop-shipment line, so a purchase order containing several item lines could create the freight shipment line more than once. If you use that event, you would need explicit duplicate protection.
One additional recommendation: add a Boolean such as "Auto Ship with Drop Shipment" to the sales line instead of identifying freight only by the /FREIGHT/ posting group. Posting groups are accounting setup; using a dedicated field makes the intended business behaviour clearer and prevents unrelated freight lines from being shipped accidentally.