Skip to main content
X++ Development Power Platform Integrations Administration · · 2 min read

Copy Attachments from Vendor Invoice Information to Vendor Open Transactions

Copy Attachments from Vendor Invoice Information to Vendor Open Transactions

A

Ahmad Hassan

Dynamics 365 F&O Consultant

Share
Copy Attachments from Vendor Invoice Information to Vendor Open Transactions

This customization copies document attachments from a VendInvoiceInfoTable record to its related VendTransOpen record after the vendor invoice information is updated. It identifies the related vendor transaction and prevents duplicate attachment links before creating the required DocuRef records.

X++ Code

class ED_CopyAttachmentsFromPOToVendorOpenInvoice 
 
{ 
    public static void copyAttachmentsPOTOVendorInvoice(VendInvoiceInfoTable _sourceInvoice, VendTransOpen _VendTransOpen) 
 
    { 
 
        DocuRef     srcDocuRef; 
 
        DocuRef     trgDocuRef; 
 
        ttsBegin; 
 
        while select srcDocuRef 
 
        where srcDocuRef.RefTableId == _sourceInvoice.TableId 
 
           && srcDocuRef.RefRecId   == _sourceInvoice.RecId 
 
        { 
 
            // Duplicate check (recommended) 
 
            select firstOnly RecId from trgDocuRef 
 
            where trgDocuRef.RefTableId == _VendTransOpen.TableId 
 
               && trgDocuRef.RefRecId   == _VendTransOpen.RecId 
 
               && trgDocuRef.ValueRecId == srcDocuRef.ValueRecId; 
 
            if(trgDocuRef.RecId) 
 
            { 
 
                continue; 
 
            } 
 
            // Copy DocuRef (link) 
 
            trgDocuRef.clear(); 
 
            trgDocuRef.RefTableId    = _VendTransOpen.TableId; 
 
            trgDocuRef.RefRecId      = _VendTransOpen.RecId; 
 
            trgDocuRef.ValueRecId    = srcDocuRef.ValueRecId; 
 
            trgDocuRef.TypeId        = srcDocuRef.TypeId; 
 
            trgDocuRef.Name          = srcDocuRef.Name; 
 
            trgDocuRef.Notes         = srcDocuRef.Notes; 
 
            trgDocuRef.Restriction   = srcDocuRef.Restriction; 
 
            trgDocuRef.RefCompanyId  = curext();; 
 
            trgDocuRef.insert(); 
 
        } 
 
        ttsCommit; 
 
    } 
 
} 
 
[PostHandlerFor(tableStr(VendInvoiceInfoTable), tableMethodStr(VendInvoiceInfoTable, update))] 
 
public static void VendInvoiceInfoTable_update_Post(XppPrePostArgs args) 
 
{ 
 
    VendInvoiceInfoTable vendInvoiceInfo = args.getThis(); 
 
    VendTrans           vendTrans; 
 
    VendTransOpen VendTransOpenV1; 
 
    select firstOnly vendTrans 
 
        join VendTransOpenV1 
 
    where vendTrans.Invoice         == vendInvoiceInfo.Num 
 
       && vendTrans.AccountNum      == vendInvoiceInfo.InvoiceAccount 
 
       && VendTransOpenV1.RefRecId  == vendTrans.RecId; 
 
    if (vendTrans.RecId) 
 
    { 
 
        ED_CopyAttachmentsFromPOToVendorOpenInvoice::copyAttachmentsPOTOVendorInvoice(vendInvoiceInfo, VendTransOpenV1); 
 
    } 
 
} 

Process Flow

  1. The post-handler is triggered after VendInvoiceInfoTable.update() executes.

  2. The related VendTrans and VendTransOpen records are identified using the invoice number and account.

  3. Existing DocuRef links are checked to prevent duplicate attachments.

  4. Each source attachment is linked to the related VendTransOpen record

Figure: Flow of copying vendor invoice attachments to the related vendor open transaction.

Found this useful?

Share it with your network

A

Written by

Ahmad Hassan

Microsoft Dynamics 365 & Power Platform blogger.

More about me

Related Posts

Comments

Leave a comment