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

D365 Sending Email with Customer Account Statement SSRS report as attachment using X++

D365 Sending Email with Customer Account Statement SSRS report as attachment using X++ custTable _custTable;        &...

2 years ago

clicking link on info message X++ to Open form

 Message::AddAction() method can be used to embed an action within a message sent to the message bar. This method supports adding a singl...

2 years ago

D365 F&O - Create custom workflow in Dynamics 365

This guide will explain every step needed to create your custom workflow in Dynamics 365. Create Enum The base enum is used to define the ...

3 years ago

D365 FO Finance and Operations : AOT Browser

  In Dynamics 365 Finance and Operations the AOT is not accessible from the user interface It is only available in a development bo...

3 years ago

D365 FO - Extensible Data Security (XDS)

This post covers D365 FO's Extensible Data Security (XDS) system, which enables the creation of flexible data-security policies beyond the def...

3 years ago

Power BI with D365FO

If you're looking to integrate Power BI with D365FO, this video tutorial is a great resource to get you started. The video tutorial explains t...

3 years ago

D365FO - SQL get security details (such as Security roles, duties and privileges)

Learn how to obtain detailed information about security roles, duties, and privileges in D365FO through SQL queries in this post. Whether you ...

3 years ago

D365 F&O LCS Generate SAS link to Download FASTER large bacpac file

If you've encountered difficulties with downloading large backup .bacpac files from LCS, this tutorial may come in handy! The author shares a ...

3 years ago

Form Control Event Handler Methods in Dynamics 365

 For example, In Dynamics 365 for Operations you can react to the OnClicked event by copying the event handler method for the event and p...

3 years ago

D365 FO Computed column in View ( Returning Field in View )

The technique to add a computed column begins with you adding a static method to the view. The method must return a string. The system automat...

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