×
Menu
Index

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::CodeunitCodeunit::"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::CodeunitCodeunit::"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::TableDatabase::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::TableDatabase::"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::TableDatabase::"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::TableDatabase::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;
}