Loading...

X++ Upload and Download file - D365 FO

X++ Upload and Download file - D365 FO
class RetailSharedParameters_Eventhandlers
{
    /// <summary>
    /// Event handler for the "UploadFile" button click event.
    /// </summary>
    /// <param name="sender">The form control that triggered the event.</param>
    /// <param name="e">Event arguments.</param>
    [FormControlEventHandler(formControlStr(RetailSharedParameters, UploadFile), FormControlEventType::Clicked)]
    public static void UploadFile_OnClicked(FormControl sender, FormControlEventArgs e)
    {
        // Get the button that triggered the event
        FormButtonControl callerButton = sender as FormButtonControl;
        
        // Get the form run instance
        FormRun form = callerButton.formRun();
        
        // Get the data source for the RetailSharedParameters table
        FormDataSource RetailSharedParameters_ds = form.dataSource(formDataSourceStr(RetailSharedParameters, RetailSharedParameters)) as FormDataSource;
        
        // Get the current record from the data source
        RetailSharedParameters RetailSharedParameters = RetailSharedParameters_ds.cursor();

        // Prompt the user to upload a file
        FileUploadTemporaryStorageResult result = File::GetFileFromUser() as FileUploadTemporaryStorageResult;
        
        // Check if the file upload was successful
        if (result && result.getUploadStatus())
        {
            // Store the file name and download URL in the table
            RetailSharedParameters.FileName = result.getFileName();
            RetailSharedParameters.SoundFile = result.getDownloadUrl();
            
            // Save the changes to the database
            RetailSharedParameters.write();
        }
    }

    /// <summary>
    /// Event handler for the "DownloadFile" button click event.
    /// </summary>
    /// <param name="sender">The form control that triggered the event.</param>
    /// <param name="e">Event arguments.</param>
    [FormControlEventHandler(formControlStr(RetailSharedParameters, DownloadFile), FormControlEventType::Clicked)]
    public static void DownloadFile_OnClicked(FormControl sender, FormControlEventArgs e)
    {
        // Variable to store the file URL
        str fileUrl = '';
        
        // Get the button that triggered the event
        FormButtonControl callerButton = sender as FormButtonControl;
        
        // Get the form run instance
        FormRun form = callerButton.formRun();
        
        // Get the data source for the RetailSharedParameters table
        FormDataSource RetailSharedParameters_ds = form.dataSource(formDataSourceStr(RetailSharedParameters, RetailSharedParameters)) as FormDataSource;
        
        // Get the current record from the data source
        RetailSharedParameters RetailSharedParameters = RetailSharedParameters_ds.cursor();
        
        // Retrieve the stored file URL
        fileUrl = RetailSharedParameters.SoundFile;
        
        // Open the file URL in a browser
        if (fileUrl)
        {
            new Browser().navigate(fileUrl);
        }
        else
        {
            // Notify the user if no file URL is found
            warning("No file URL found.");
        }
    }
}

Published on:

Learn more
Sherif Fayed
Sherif Fayed

Share post:

Related posts

New Approved Customer List mapping for Items in Dynamics 365 Finance and operations

Take control of your sales process with the latest feature in Microsoft Dynamics 365 Finance – the Approved Customer List! This powerful new f...

2 days ago

Part-9: Connect and Query Dynamics 365 Finance and operations data with Copilot studio

In this video, we kick off an exciting new series focused on extending Copilot Studio capabilities by connecting it to real enterprise data! �...

2 days ago

How to Create Deep Links to D365 F&O Records with Dataverse Virtual Tables

It is a common requirement for SaaS platform that exposes form over data to be able to share a secure link (a.k.a. Deep Link) to access a give...

2 months ago

Simplifying License Management for Dynamics 365 Finance and Operations: Improved User License Validation

Starting April 30th, Dynamics 365 administrators will gain access to new license usage reporting tools, centralizing user license management. ...

3 months ago

D365FO Integration: Import Sales Orders from an External Web Application

Learn how to implement robust and efficient process to import complex documents into Dynamics 365 Finance and Operations from external Web ser...

4 months ago

Practical Hints for Technical Management of D365FO Go-Live

In this post, I share practical insights from my experience managing the technical side of Go-Live for D365FO projects, focusing on key activi...

8 months ago

D365FO Integration: Event-Based Exports to External Web Services

How to implement robust, efficient integrations between Dynamics 365 Finance and Operations and external Web Services. This post covers design...

9 months ago
Stay up to date with latest Microsoft Dynamics 365 and Power Platform news!
* Yes, I agree to the privacy policy