Attaching a document to a purchase order using x++ job (Runnable class) in Dynamics 365 for Finance and Operations

In previous post, the file attachment is done with the file selected by User from UI. Instead, if the file path is known, can directly pass same in x++ to attach to PurchTable record like below.
class Jobs
{
    public static void main(Args _args)
    {
        str                     imageToSave = "C:\\invoice.jpg";
        PurchTable              purchaseTable;
        System.Net.WebClient    webClient;
        System.IO.Stream        stream;

        if (imageToSave && System.IO.File::Exists(imageToSave))
        {
            using (System.IO.FileStream fileStream = newSystem.IO.FileStream(imageToSave, System.IO.FileMode::Open, System.IO.FileAccess::Read))
            {
                stream = fileStream;
                stream.Seek(0, System.IO.SeekOrigin::Begin);
               
  select purchaseTable where purchaseTable.PurchId == "000006"; // select record to which you want to attach
                DocuRef docuref =DocumentManagement::attachFile(purchaseTable.TableId, purchaseTable.RecId, purchaseTable.DataAreaId,DocuType::typeFile(),stream, System.IO.Path::GetFileName(imageToSave), System.Web.MimeMapping::GetMimeMapping(imageToSave), System.IO.Path::GetFileName(imageToSave));
                // download/view the attachment
                str displayUrl =DocumentManagement::getAttachmentPublicUrl(docuref);
                Browser br = new Browser();
                br.navigate(displayUrl);
            }
        }
    }


}

Comments

Popular posts from this blog

Dynamics 365 Finance & Operations Release pipelines import.

Attaching a user selected document to a purchase order using x++ job (Runnable class) in Dynamics 365 for Finance and Operations

Claude AI Models Comparison (Updated: July 2026): Haiku 4.5 vs Sonnet 5 vs Opus 4.8 vs Fable 5