web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Asynchronous Uploaded File Handling in AX7

(0) ShareShare
ReportReport
Posted on by

Hey guys,

Problem statement: Let user upload excel file through the form.

What I want: The file to be uploaded and then handled in a background thread so that users can freely switch pages or even close the browser, while the file is being handled and inserted into the database. Once finished the callback function update the infolog, so when the user returns, he is informed that the file was uploaded.

  

What have I done: Implemented a form with a FileUpload button, whilst overriding its onUploadCompleted() method to handle post operation. As well as implemented an Excel file handler class (which is working file when the file is uploaded synchronously). 

Where am I struggling: I have found FormRun.runAsync() method can be used to execute the logic in the background. The parameter definition is as: 

public System.Threading.Tasks.Task runAsync(int runAsClassId, str runAsStaticMethodName, 
[container params], [System.Threading.CancellationToken], [str callbackFormMethodName],
and some more that I don't have use of);

The problem occurs when I call the function as:

public void onUploadCompleted()
{
super();
container con = conNull(); // con = conIns(con, 1, this); // This gives error // ExcelImport::fileUpload = this // runAsync treats this as null element.runAsync(classNum(ExcelImport), staticMethodStr(ExcelImport, handleImportedFile), con, System.Threading.CancellationToken::None, formMethodStr(ImportForm, importCallback));
}


According to my knowledge, setting the static fileUpload field should have worked, but is treated as a nullObject when I debug the code, so the other way to handle this was to pass the FileUpload instance in the container as a parameter, but FileUpload being an object is rejected by the container, as they can't hold objects. How do I solve this problem?


Note: The ExcelImport::handleImportedFile() works perfectly if performed synchronously. 

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at

    Even if it worked, changing a static field without any check for concurrent access wouldn't be thread-safe.

    Why do you need the fileUpload instance? Wouldn't the file URL be sufficient? Also, when I looked for details how to find the URL, I realized that FileUploadResultBase (which you'll get by calling fileUpload.getFileUploadResult() is a data contract class, therefore you could serialize the whole result.

  • Community Member Profile Picture
    on at

    This is the reason passing it as a parameter was my first priority, since it didn't work, had to look for alternatives to resolve the problem. The other way was to pass the URL and then use the file via URL and work with it, but I don't possess the knowledge to implement that.

    I need it to fetch the results in a stream and pass it to the Excel parser which henceforth writes each row in the Excel file to the database. It would be, but I don't know how to handle that.

  • Community Member Profile Picture
    on at

    Ok, I went through your blog and saw that we can use result.getDownloadUrl() to pull URL of the uploaded file & then use the File::useFileFromUrl() method to get the file as System.IO.Stream, which are easy for OfficeOpenXml.ExcelPackage namespace to handle.

    I still have a query. Once my file is uploaded in the background thread (lets suppose user closed the browser at that time), how would I notify users when they come back again? can the notification pane be configured in a way that you can push notification and it stays there until the user reads them?

  • Suggested answer
    Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at

    Getting a stream from the URL is easy - you can simply call File::UseFileFromURL().

    But if you don't want to deal with the URL directly, it sounds like another reason why you should consider serializing the file result object with a data contract serializer. After deserialization, you'll simply call openResult().

  • Verified answer
    Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at

    Put messages to infolog, they'll be displayed in Message Center. You'll find more detail in documentation under Message center, message bar, and message details FAQ.

  • Community Member Profile Picture
    on at

    Yes I opted for File::UseFileFromUrl(), rather than serializing and deserializing since it got the job done.

    Grats, I'll give it a read. Even though I noticed that callback automatically inserts any message to the Message Center due to the default nature of async process message handling.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans