Thursday, August 11, 2016

Attaching a user selected document to a purchase order using x++ job (Runnable class) in Ax7

While working in document management in Ax 7, For testing purposes I wanted to attach documents to some posted tables and written a simple program to get files from user and attach to the record. Modified same to show how it work with PurchTable.

class Jobs
{
    public static void main(Args _args)
    {
       PurchTable              purchaseTable;
       System.Net.WebClient    webClient;
       System.IO.MemoryStream  stream;

FileUploadTemporaryStorageResult fileUploadResult = File::GetFileFromUser (classstr(ImageFileUploadTemporaryStorageStrategy));
       
        if (fileUploadResult && fileUploadResult.getUploadStatus())
        {
            str imageFilePathName = fileUploadResult.getDownloadUrl();

            InteropPermission perm = new InteropPermission(InteropKind::ClrInterop);
            perm.assert();
            
            // BP Deviation Documented
            webClient = new System.Net.WebClient();
            // BP Deviation Documented
            stream = new System.IO.MemoryStream(webClient.DownloadData(imageFilePathName));
           
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,fileUploadResult.getFileName(), fileUploadResult.getFileContentType(),fileUploadResult.getFileName());
            // download/view the attachment
            str displayUrl = DocumentManagement::getAttachmentPublicUrl(docuref);
            Browser br = new Browser();
            br.navigate(displayUrl);
            CodeAccessPermission::revertAssert();
        }
    }
}

No comments:

Post a Comment