⚙️ Recommended Approach: Custom Codeunit + Event Subscriber
Instead of writing a trigger directly on the `Item` table (which could get messy or block upgrades), **use an event subscriber on the `OnAfterPostPurchaseDocument` event**, then reset the `Last Direct Cost` programmatically.
💡 Why this is safer than direct triggers:
- Keeps core table logic untouched
- Easier to upgrade and maintain
- You can scope this logic narrowly (only on posted Purchase Invoices)
---
🔧 Sample Logic Outline (AL Code)
```al
[EventSubscriber(ObjectType::Codeunit, Codeunit::"Purch.-Post", 'OnAfterPostPurchaseDocument', '', false)]
procedure ResetLastDirectCostAfterPO(var PurchHeader: Record "Purchase Header"; var GenJnlLine: Record "Gen. Journal Line")
var
PurchLine: Record "Purchase Line";
ItemRec: Record Item;
begin
if PurchHeader."Document Type" = PurchHeader."Document Type"::Invoice then begin
PurchLine.SetRange("Document No.", PurchHeader."No.");
PurchLine.SetFilter(Type, '%1', PurchLine.Type::Item); // Only item lines
if PurchLine.FindSet() then
repeat
if ItemRec.Get(PurchLine."No.") then
ItemRec."Last Direct Cost" := 0;
ItemRec.Modify();
until PurchLine.Next() = 0;
end;
end;
```
You can also add filtering logic for item category or vendor if you want to apply this selectively.
Please try this in Sandbox environment.