D365FO – Create a Form button to upload images into attachments through X++

 I want to create a button on a form to allows users to upload images into D365FO attachments.

To do that I can use the “FileUpload Strategy class” property of the FileUpload Form control.

So create the new control

In the FileUpload Strategy class choose “ImageFileUploadTemporaryStorageStrategy” value.

In the FileNameLabel set the name

A control like this will be shown in the form

Now override the “OnUploadCompleted” control method

Now copy and paste this code

    [Control("Custom")]
    class ImageUpload
    {
        public void OnUploadCompleted()
        {
            FileUploadTemporaryStorageResult    result = this.getFileUploadResult() as FileUploadTemporaryStorageResult;
              
            super();

            if(result && result.getUploadStatus())
            {       
                str fileName = "YourFileName.jpeg"; //Insert the name of the attachchment

                using(System.IO.MemoryStream stream = result.openResult() as System.IO.MemoryStream)      
                {                                     
                    DocuRef docuRef = DocumentManagement::attachFile(                            
                            YourTable.TableId, // The Table Id that will be linked to the the attachment
                            YourTable.RecId,   //The RecId of the record that will be linked to the attachment 
                            curext(),
                        DocuType::typeImage(), //Input here the attachment type 
                            stream,
                            fileName,
                            null,
                            fileName);
                }
            }

            this.fileName("");
        }
    }

Now build the solution and run the form

Click on Browse button, choose a file

and upload it

Now click on “Attachment” button and you’ll find your attachment

Comments