Check Whether Original Transaction Has Attachments from Voucher Transactions in D365FO

 

Purpose

The purpose of this document is to demonstrate how to identify whether an original transaction associated with a voucher contains any document attachments in Microsoft Dynamics 365 Finance and Operations.

This functionality can be useful when:

  • Displaying an attachment indicator in Voucher Transactions form.
  • Enabling users to quickly identify transactions that have supporting documents attached.
  • Extending voucher inquiry forms with attachment status.

Product

Microsoft Dynamics 365 Finance and Operations (D365FO)


Development Approach

The solution leverages the standard D365FO framework classes:

  • GeneralJournalEntry
  • GeneralJournalAccountEntry
  • OriginalDocuments
  • TmpLedgerBase
  • DocuRef

The logic follows the same approach used by the standard Voucher Transactions > Original Documents functionality.


 class IsOrigTransHadAttchment
{
    /// <summary>
    /// Class entry point. The system will call this method when a designated menu 
    /// is selected or when execution starts and this class is set as the startup class.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {
        boolean              hasAttachment = false;
        GeneralJournalAccountEntry accountEntry;
        GeneralJournalEntry entry;
        OriginalDocuments    originalDocuments;
        LedgerTransModule    ledgerTransModule;
        TmpLedgerBase        tmpLedgerBase;
        Common               common;
        DocuRef              docuRef;


        // 1) Get one account entry for this journal entry (any line is fine as a starting point)
        select firstOnly accountEntry
            join entry where accountEntry.GeneralJournalEntry == entry.RecId
            && entry.SubledgerVoucher == "FY25-004405";


        common = accountEntry; // OriginalDocuments takes Common


        // Try OriginalDocuments first (standard "Original document" logic from Voucher transactions)
        originalDocuments = new OriginalDocuments(common);
        originalDocuments.findRelations();
        tmpLedgerBase.setTmpData(originalDocuments.relations());


        // If there are no original docs, fall back to transaction origins
        /*if (!tmpLedgerBase.RecId)
        {
           // ledgerTransModule = new LedgerTransModule();
            ledgerTransModule.createTransModule(accountEntry.RecId);
            tmpLedgerBase.setTmpData(ledgerTransModule.transModule());
        }    */


        // 3) Loop all origin records and check Document management (DocuRef)
        while select tmpLedgerBase
        {
            // tmpLedgerBase.RefTableId / RefRecId describe the original record
            select firstOnly docuRef
            where docuRef.RefTableId  == tmpLedgerBase.TableId
              && docuRef.RefRecId     == tmpLedgerBase.RecId
              && docuRef.RefCompanyId == tmpLedgerBase.DataAreaId;


            if(!docuRef)
            {
                select firstOnly docuRef
                    where docuRef.RefRecId     == tmpLedgerBase.RecordRecId
                      && docuRef.RefCompanyId == entry.SubledgerVoucherDataAreaId;
            }


            if (docuRef.RecId)
            {
                hasAttachment = true;
               
            }
        }
    }
}

Comments