How to implement NavSherpa functions with Events
NavSherpa contains
NavSherpa Interface (Codeunit 4065007), sometimes called the
NavSherpa API. This NavSherpa module provides functions that can be used for managing data relations from code.
Example of how to use the functions from AL code
codeunit 4065499 "NSP Demo Event Subscriptions"
{
// version NSPW113.00.03
trigger OnRun()
begin
end;
var
NotApprovedErr: Label 'The result of workflow "%1" must be ''%2''.', Comment = '%1 = Workflow Name, %2 = Workflow Status';
ApprovedLbl: Label 'Approved';
[EventSubscriber(ObjectType::Codeunit, Codeunit::"Purch.-Post", 'OnBeforePostPurchaseDoc', '', true, true)]
local procedure OnBeforePostPurchaseDoc(var PurchaseHeader: Record "Purchase Header")
var
NavSherpaInterface: Codeunit "NSP Interface";
EmptyRecordReference: RecordRef;
NotApprovedWFName: Text;
begin
if PurchaseHeader."Document Type" = PurchaseHeader."Document Type"::Invoice then begin
if not NavSherpaInterface.IsAllWorkflowsApproved(PurchaseHeader,'',NotApprovedWFName) then
Error(NotApprovedErr,NotApprovedWFName,ApprovedLbl);
NavSherpaInterface.CheckCopyMapping(NSP_SPListType::Library,PurchaseHeader,'','',EmptyRecordReference,'',DATABASE::"Purch. Inv. Header",'',false);
end;
end;
[EventSubscriber(ObjectType::Codeunit, Codeunit::"Purch.-Post", 'OnAfterPostPurchaseDoc', '', true, true)]
local procedure OnAfterPostPurchaseDoc(var PurchaseHeader: Record "Purchase Header";var GenJnlPostLine: Codeunit "Gen. Jnl.-Post Line";PurchRcpHdrNo: Code[20];RetShptHdrNo: Code[20];PurchInvHdrNo: Code[20];PurchCrMemoHdrNo: Code[20])
var
PurchInvHdr: Record "Purch. Inv. Header";
NavSherpaInterface: Codeunit "NSP Interface";
begin
if PurchaseHeader."Document Type" = PurchaseHeader."Document Type"::Invoice then begin
PurchInvHdr.Get(PurchInvHdrNo);
NavSherpaInterface.CopyDocumentMapping(PurchaseHeader,'','',PurchInvHdr,'','',false);
NavSherpaInterface.DeleteDocumentMapping(false,PurchaseHeader,'');
end;
end;
[EventSubscriber(ObjectType::Table, Database::Currency, 'OnAfterDeleteEvent', '', true, true)]
local procedure OnAfterCurrencyDeleteEvent(var Rec: Record Currency;RunTrigger: Boolean)
var
NavSherpaInterface: Codeunit "NSP Interface";
begin
if Rec.IsTemporary then
exit;
NavSherpaInterface.DeleteRecordMapping(Rec,'');
end;
[EventSubscriber(ObjectType::Table, Database::"Purchase Header", 'OnAfterDeleteEvent', '', true, true)]
local procedure OnAfterPurchaseHeaderDeleteEvent(var Rec: Record "Purchase Header";RunTrigger: Boolean)
var
PurchInvHeader: Record "Purch. Inv. Header";
NavSherpaInterface: Codeunit "NSP Interface";
begin
if Rec.IsTemporary then
exit;
if Rec."Document Type" = Rec."Document Type"::Invoice then begin
PurchInvHeader.SetRange("Pre-Assigned No.",Rec."No.");
if PurchInvHeader.IsEmpty() then
NavSherpaInterface.DeleteDocumentMapping(false,Rec,'');
NavSherpaInterface.DeleteRecordMapping(Rec,'');
NavSherpaInterface.DeleteWorkflowMapping(Rec,'');
end
else begin
NavSherpaInterface.DeleteRecordMapping(Rec,'');
NavSherpaInterface.DeleteDocumentMapping(false,Rec,'');
NavSherpaInterface.DeleteWorkflowMapping(Rec,'');
end;
end;
[EventSubscriber(ObjectType::Table, Database::"Salesperson/Purchaser", 'OnAfterDeleteEvent', '', true, true)]
local procedure OnAfterSalespersonDeleteEvent(var Rec: Record "Salesperson/Purchaser";RunTrigger: Boolean)
var
NavSherpaInterface: Codeunit "NSP Interface";
begin
if Rec.IsTemporary then
exit;
NavSherpaInterface.DeleteRecordMapping(Rec,'');
NavSherpaInterface.DeleteDocumentMapping(false,Rec,'');
end;
[EventSubscriber(ObjectType::Table, Database::Vendor, 'OnAfterDeleteEvent', '', true, true)]
local procedure OnAfterVendorDeleteEvent(var Rec: Record Vendor;RunTrigger: Boolean)
var
NavSherpaInterface: Codeunit "NSP Interface";
begin
if Rec.IsTemporary then
exit;
NavSherpaInterface.DeleteRecordMapping(Rec,'');
NavSherpaInterface.DeleteDocumentMapping(false,Rec,'');
end;
}