Archiving Customization

<< Click to Display Table of Contents >>

Navigation:  Zetadocs SDK Guide > Creating a Per Tenant Extension >

Archiving Customization

You can customize the archiving process to fit your requirements. For instance

 

Add Archive Metadata Example

This example shows how to use the OnBeforeArchive Event to add some metadata that will be stored with the document in SharePoint Online. Here the 'Sales Person Code' is added as metadata for all documents that use the 'Sales Header' table and the 'Purchaser Code' is added for all documents that use the 'Purchase Header' table.

 

   [EventSubscriber(ObjectType::CodeunitCodeunit::"Zetadocs Customize", 'OnBeforeArchive''', true, true)]

    procedure OnBeforeArchive(var onBeforeArchiveHandler: Codeunit "Zetadocs OnBeforeArchive")

    var

 SalesHeader: Record "Sales Header";

 PurchaseHeader: Record "Purchase Header";

 RecordId: RecordId;

 TableNo: Integer;

   begin

 // Get RecordId the file is being archived against

 onBeforeArchiveHandler.GetRecordId(RecordId);

 TableNo := RecordId.TableNo;

 

 // Change where to archive depending on the type of record

       case TableNo of

     36: // Sales Header

           begin

             if (SalesHeader.Get(RecordId)) then begin

                 // 'SalesPersonCode' must match the column name in SharePoint

                 onBeforeArchiveHandler.AddOrUpdateMetadata('SalesPersonCode', SalesHeader."Salesperson Code");

             end;

               end;

     38: // Purchase Header

         begin

            if (PurchaseHeader.Get(RecordId)) then begin

                 // 'PurchaserCode' must match the column name in SharePoint

                 onBeforeArchiveHandler.AddOrUpdateMetadata('PurchaserCode', PurchaseHeader."Purchaser Code");

             end;

         end;

       end;

 

       // Propagate any error to the caller

       onBeforeArchiveHandler.PropagateLastError();

   end;

 

 

Overriding the Archive Location Example

This example shows how to use the OnBeforeArchive Event to change where documents are archived depending on the table for the record and the document type.

 

   [EventSubscriber(ObjectType::CodeunitCodeunit::"Zetadocs Customize", 'OnBeforeArchive''', true, true)]

    procedure OnBeforeArchive(var onBeforeArchiveHandler: Codeunit "Zetadocs OnBeforeArchive")

    var

       salePersonRecord: Record "Salesperson/Purchaser";

 SalesHeader: Record "Sales Header";

 PurchaseHeader: Record "Purchase Header";

       FileManagment: Codeunit "File Management";

 RecordId: RecordId;

 TableNo: Integer;

 salesQuoteFolders: Text;

 fileName: Text;

 zdMetadata: Dictionary of [Text, Text];

 zdDocumentType: Text;

 salesPersonCode: Code[20];

   begin

 // Get RecordId the file is being archived against

 onBeforeArchiveHandler.GetRecordId(RecordId);

 TableNo := RecordId.TableNo;

 

 // Change where to archive depending on the type of record

       case TableNo of

     36: // Sales Header

           begin

             if (SalesHeader.Get(RecordId)) then begin

                       salesPersonCode := SalesHeader."Salesperson Code";

                       

                 // Get the value of the 'ZetadocsRecordType' metadata for use in the folder structure

                 onBeforeArchiveHandler.GetAdditionalMetadata(zdMetadata);

                 zdMetadata.Get('ZetadocsRecordType', zdDocumentType);

 

                 // Set the folder e.g. PS/Sales Quote/

                       salesQuoteFolders := salesPersonCode + '/' + zdDocumentType + '/';

                       onBeforeArchiveHandler.SetSubFolders(salesQuoteFolders);

 

                 // Set the target library

                 onBeforeArchiveHandler.SetTargetLibrary('Sales Documents');

 

                 // if the document is a Sales Order then also append the sales person's name to the filename.

                 if (SalesHeader."Document Type" = SalesHeader."Document Type"::Order) then begin

                     salePersonRecord.Get(salesPersonCode);

                     onBeforeArchiveHandler.GetFileName(fileName);

                           fileName := FileManagment.GetFileNameWithoutExtension(fileName);

                     onBeforeArchiveHandler.SetFileName(fileName + ' - ' + salePersonRecord.Name);

                 end;

                   end;

         end;

 

     38: // Purchase Header

         begin

                  if (PurchaseHeader.Get(RecordId)) then begin

                 // Set the SharePoint site and target library

                 onBeforeArchiveHandler.SetSharePointSite('https://example.sharepoint.com/sites/yourSiteCollection/SubSite');

                       onBeforeArchiveHandler.SetTargetLibrary('Purchase Documents');

                  end;

         end;

 end;

 

 // Propagate any error to the caller

       onBeforeArchiveHandler.PropagateLastError();

    end;