OnBeforeDeleteEvent not firing when Sales Line is deleted

Hi,  

I am trying to update Available Stock quantity based on the Sales Lines in Sales Order.  When the Sales line is inserted or modified, the Available stock quantity field gets calculated correctly but when the sales line is deleted, the OnBeforeDeleteEvent or OnBeforeDelete are not triggering to calculate the Available Stock quantity field.  Below is the code I have used to update the field when the Sales Line is deleted.  Can anyone let me know why the delete event not triggered or anything wrong with the coding?  Thanks and Regards.

  [EventSubscriber(ObjectType::Table, Database::"Sales Line", 'OnBeforeDeleteEvent', '', true, true)]

    local procedure OnBeforeDeleteEvent(var Rec: Record "Sales Line");

    var
        ItemRec: Record Item;
    begin
        ItemRec.SetAutoCalcFields(Inventory, "Qty. on Sales Order");
        ItemRec.SetAutoCalcFields("Qty. on Purch. Order");
        ItemRec.SetRange("No.", Rec."No.");

        if ItemRec.FindSet(true, true) then begin

            ItemRec."Available Stock Qty_CRM" := ItemRec.Inventory + ItemRec."Qty. on Purch. Order" - ItemRec."Qty. on Sales Order";
            ItemRec.Modify();

        end;
    end;

Related
Recommended