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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

Throwing error exception and file not getting downloaded

(1) ShareShare
ReportReport
Posted on by
Hi team,
 
I have written a batch job class to download a excel file. However, it throws an exception given below:

 
This is the piece of code, I am using to download the file.

And it generates a download url but when it goes to br.navigate method it trows and error pasted above.

below is the call stack.
 
Can you please guide. Also I noticed that memorystream shows ReadTimeout = 'memoryStream.ReadTimeout' threw an exception of type 'System.InvalidOperationException'.
I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    238,208 Most Valuable Professional on at
    SendFileToUser() is used in interactive sessions to give a file to a user for download. But there is no user when the process runs in a batch, therefore there is no one to download the file. The whole logic is wrong.
     
    What you can do in a batch is saving the file somewhere and later allow a user to download it. You can see an example in data management, when data is export in a batch, the file is stored in the cloud and there is button that user can use to download it.

    (Moved from Dynamics AX forum.)
  • Karuna Dogra Profile Picture
    on at
    Thank you for responding Martin. 
     
    Is there any other way to download the file in batch other then sendFileToUser() ? If yes, how I can achieve that?
     
     If not then is there any page/ existing thread for this where I can get some example to store file on cloud. Also, for this i will need cloud location from user/client to save the file right?
     
    Please guide.
  • Martin Dráb Profile Picture
    238,208 Most Valuable Professional on at
    No, if you run it without a user, you can't give it to a user for download. Not only it's not technically possible, but the user doesn't have to be using F&O at the moment at all.
     
    Either do what I suggested, or don't use a batch.
     
    Normally you don't ask a user for location. If you look at data manegement, as I suggested, you can see that the user doesn't choose a cloud location. The file is stored somewhere in the cloud, where exactly isn't user's interest. What user chose is a local location when (if) he decides to download the file.
     
    How to implement it depends on what storage you decide to use. You can use the internal blob storage, but you could also use your own blob storage, Azure file storage, send the data to a logic app (and utilize any connector there) and so on. If you want just a temporary location before a user downloads the file, the internal blob storage would make the best sense to me.
  • Karuna Dogra Profile Picture
    on at
    Hi Martin,
     
    Earlier it was a normal excel export through which we were exporting the production qty data. But that export was taking time and user has to wait until the processing completes. That's why I converted it to batch job, so that it does not turn into time out or user does not has to wait until the processing end.
     
    Therefore, this batch export will not be setup in recurrence for the time when user is not using the system. User will always run it when he is working on the system. Its just that he will not required to wait for processing to get completed.

    1. In this case, can we pass current user to batch and download it on user's local machine? Is this possible and how I can achieve it?

    2. If only blob storage is only way, can you please guide me how I can access internal blob storage and how we can save the file there?
     
    Please help and thank you for your time.
  • Suggested answer
    Martin Dráb Profile Picture
    238,208 Most Valuable Professional on at
    1) No, it's not possible. As I said, there is no interactive user session on the batch server.
    2) A blob storage isn't the only way. If you read my previous replies, you'll see that I mentioned different kinds of storage too.
    Regarding using the internal blob storage for temporarily storing the file, you can see an example in your screenshot. It calls File::SendFileToTempStore(). By the way, pay attention to the third parameter, fileUploadStrategyClassName. The default is FileUploadTemporaryStorageStrategy, which has FileTimeOutInMinutes = 10. You may want to create a custom strategy with a longer time.
  • Karuna Dogra Profile Picture
    on at
    >>Regarding using the internal blob storage for temporarily storing the file, you can see an example in your screenshot. It calls File::SendFileToTempStore(). By the way, pay attention to the third parameter, fileUploadStrategyClassName. 
     
    1. Can I pass my stream to this File::SendFileToTempStore() directly ?? but then from which location user will download the file (I didn't understand  on which location is it storing the file ).
     
     
    This is code, I am using right now.
       
            memoryStream.Seek(0,System.IO.SeekOrigin::Begin);
    
            str     fileName    = strFmt('GlobalProductionQty-%1-%2', starttime, DateTimeUtil::utcNow());
    
            //Download the file.
            DocuFileSaveResult          saveResult      = DocuFileSaveResult::construct();
            saveResult.parmAction(DocuFileSaveAction::Download);
            saveResult.parmFilename(strFmt('%1.xlsx', fileName));
            DocuFileSave::processSaveResult(memoryStream,saveResult);
    
    should I directly call this method File::SendFileToTempStore() here in my code?
  • Martin Dráb Profile Picture
    238,208 Most Valuable Professional on at
    The return value of SendFileToTempStore() is the download URL. You'd present it to the user in some way.
  • Karuna Dogra Profile Picture
    on at

    >>The return value of SendFileToTempStore() is the download URL. You'd present it to the user in some way.

    Now, I have created a table in which I am storing this download url. I have created a form and added a button "download excel "on it so that user can select the record in the grid and download the file.

    I am writing the below code on click of the button but its throwing error ": 'Unable to cast object of type 'Dynamics.AX.Application.xyzGlobalProductionOrdersQty' to type 'Dynamics.AX.Application.xyzGlobalProdQtyExcelURL'.'"
    [Form]
    public class xyzExcelDownLoadForm extends FormRun
    {
        [Control("CommandButton")]
        class DownloadExcel
        {
            /// <summary>
            /// 
            /// </summary>
            public void clicked()
            {
                xyzGlobalProdQtyExcelURL  urlTable = element.args().record();
                new Browser().navigate(urlTable.DownloadURL);
                super();
            }
        }
    
    }
     
    Please guide if I am doing it right or what changes I should make to fix this.
     
    Or is there a way I can override grid's method so that by directly clicking on grid url file can be downloaded as grids do not have click method.
    Please help Martin Ji.
  • Martin Dráb Profile Picture
    238,208 Most Valuable Professional on at
    In addition to knowing what error you're getting, you should find which line of code is throwing it.
     
    My guess is that the problem is here:
    xyzGlobalProdQtyExcelURL  urlTable = element.args().record();
    Your code assumes that record() returns a xyzGlobalProdQtyExcelURL record, but according to the error message, it actually returns a xyzGlobalProductionOrdersQty record. Use the debugger to verify it.
     
    Also, according to your screenshot, it seems that you want to use a data source of the form, not a record passed from another form when opening this form.
     
     
  • Karuna Dogra Profile Picture
    on at
    Yes thats right, I want to use data source of the form, not a record passed from another form when opening this form.
     
    On my current form, I will select the grid record and then when I click on the button download excel, I want to get the current datasource's fields value(excel download url).
     
    Is this statement right for that?
     
    xyzGlobalProdQtyExcelURL  urlTable = element.args().record();

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

News and Announcements

Season of Giving Solutions is Here!

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 > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Abhilash Warrier Profile Picture

Abhilash Warrier 763 Super User 2025 Season 2

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 413 Super User 2025 Season 2

#3
Martin Dráb Profile Picture

Martin Dráb 284 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans