- Open the alExtensibleAppProject folder in Visual Studio Code.
- In Explorer, select Transaction Entry.al. In the Editor, add a new event publisher to the table:
[IntegrationEvent(false, false)]
local procedure OnBeforeGetLastDirectUnitCost(var TransactionEntry: Record "Transaction Entry"; var IsHandled: Boolean)
begin
end;
- In the GetLastDirectUnitCost() function, create a new local variable:
IsHandled: Boolean;
- Now, raise the event at the start of the GetLastDirectUnitCost() function:
OnBeforeGetLastDirectUnitCost(Rec, IsHandled);
- After the call to the event, add logic to exit the function if the subscriber has set IsHandled to true:
if IsHandled then
exit;
Your function should now look like this, with the new code highlighted as follows:
procedure GetLastDirectUnitCost()
var
PurchaseInvLine: Record "Purch. Inv. Line";
IsHandled: Boolean;
begin
OnBeforeGetLastDirectUnitCost(Rec, IsHandled);
if IsHandled then
exit;
clear("Unit Cost");
if "Item No." = '' then
exit;
PurchaseInvLine.SetRange(Type, PurchaseInvLine.Type::Item);
PurchaseInvLine.SetRange("No.", "Item No.");
if PurchaseInvLine.FindLast() then
validate("Unit Cost", PurchaseInvLine."Direct Unit Cost");
end;
It's that easy!
Now, a subscriber can take over the function, as well as perform its own calculation and bypass the rest of the logic within the function.