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");
ItemRec: Record Item;
ItemRec.SetAutoCalcFields(Inventory, "Qty. on Sales Order");
ItemRec.SetAutoCalcFields("Qty. on Purch. Order");
if ItemRec.FindSet(true, true) then begin
ItemRec."Available Stock Qty_CRM" := ItemRec.Inventory + ItemRec."Qty. on Purch. Order" - ItemRec."Qty. on Sales Order";
If you are only retrieving\updating 1 record, use a FINDFIRST and not FINDSET.
Thanks for highlighting on that. I will correct it accordingly.