Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

How to get excel file url or getting excel from the batch job

(0) ShareShare
ReportReport
Posted on by 35

I want to improt excel So  I created a class that extends runbasebatch, but I use the background batch to execute batch that fileUrl is null, how should I get the excel that uploaded in Temporary place at batch job,

please all  help me.

fileUrl = result.getDownloadUrl();

fileUrl is null

  • Suggested answer
    DaLianAxNewbieJJP Profile Picture
    35 on at
    RE: How to get excel file url or getting excel from the batch job

    Thank you so much. I think I know what to do and I will try to understand the sys operation framework. Thanks again

  • Verified answer
    Martin Dráb Profile Picture
    231,933 Most Valuable Professional on at
    RE: How to get excel file url or getting excel from the batch job

    I guessed it right - you forgot to implement pack() and unpack().

    They're necessary for being able to save the object state to database and re-create it again when the batch server picks the data from database and wants to execute the task.

    You said you're familiar with the RunBase framework, therefore I'm assuming that you know how to implement pack() and unpack(). If not, feel free to ask.

    By the way, one of the benefits of the SysOperaration framework is that it handles these things for you.

  • DaLianAxNewbieJJP Profile Picture
    35 on at
    RE: How to get excel file url or getting excel from the batch job

    class ExcelBGUploadTest extends RunBaseBatch

    {

       str  fileUrl;

       public static void main(Args _args)

       {

           ExcelBGUploadTest    obj;

           obj  = new ExcelBGUploadTest();

           if (obj.prompt())

           {

               obj.runOperation();

           }

       }

       server client static public ClassDescription description()

       {

           return "My batch job";

       }

       public boolean canRunInNewSession()

       {

           return false;

       }

       Object Dialog()

       {

           FormBuildButtonControl buttonControl;

           DialogGroup            dlgGroup;

           FormBuildGroupControl  buttonGroup;

           Dialog dialog = super();

           dlgGroup       = dialog.addGroup('');

           buttonGroup    = dialog.formBuildDesign().control(dlgGroup.formBuildGroup().id());

           buttonControl  = buttonGroup.addControl(FormControlType::Button, 'Upload');

           buttonControl.text("Upload file");

           buttonControl.registerOverrideMethod(methodStr(FormButtonControl, clicked),

                                           methodStr(ExcelBGUploadTest, uploadClickedEvent),

                                           this);

           return dialog;

       }

       private void uploadClickedEvent(FormButtonControl _formButtonControl)

       {

           FileUploadTemporaryStorageResult result = File::GetFileFromUser() as FileUploadTemporaryStorageResult;

           if (result && result.getUploadStatus())

           {

               result.getFileContentType();

               fileUrl = result.getDownloadUrl();

               info(fileUrl);

           }

       }

       Public  void   run()

       {

           System.Byte[] byteArray;

           System.IO.Stream     stream;

           try

           {          

               stream = File::UseFileFromURL(fileUrl);

               this. readExcelData(stream);

               info("Done");

           }

           catch(Exception::Error)

           {

               info(strFmt("%1 %2",Exception::Error,fileUrl));

           }

       }

       public container  readExcelData(System.IO.Stream      _stream)

       {

           OfficeOpenXml.ExcelWorksheet _worksheet;

           OfficeOpenXml.ExcelPackage package = new OfficeOpenXml.ExcelPackage(_stream);

           int iRowCount,iCellCount;

           anytype        anyData;

           container  conRow;

           container  conData;

           purchreqtable purchreqtable;

           try

           {

               ttsbegin;

               if(package)

               {

                   _worksheet = package.get_Workbook().get_Worksheets().Copy("Sheet1","Journal");

                   var cells = _worksheet.get_Cells();

                   //iRowCount = _worksheet.get_Dimension().get_End().get_Row();

                   //iCellCount = _worksheet.get_Dimension().get_End().get_Column();

                   info(any2Str(str2Date(cells.get_Item(1, 1).get_Value(),321)));

                   date nextDay = str2Date(cells.get_Item(1, 1).get_Value(),321);

                   select forupdate firstonly TransDate

                       from purchreqtable

                       where purchreqtable.PurchReqId == '002531';

                   purchreqtable.TransDate = nextDay;

                   purchreqtable.update();

               }

               ttscommit;    

           }

           catch (Exception::CLRError)

           {

               throw error("@SYS135884");

           }

           return conData;

       }

    }

  • Martin Dráb Profile Picture
    231,933 Most Valuable Professional on at
    RE: How to get excel file url or getting excel from the batch job

    I'm not saying that there is anything wrong with writing X++ code. But the best code is the one that you don't have to design, write, test and keep maintaining at all. Therefore if the product already can do what you need, using the existing functionality is much more efficient than building something similar by yourself.

    Can you show us your code, please? I'm particularly interested in how you construct the result object when running on batch server.

    Regarding SysOperation framework, it was introduced in AX 2012 and even if you refuse to use it by yourself, you need to get familiar with it, because it's used by Microsoft. For example, reports in AX 2012 and D365FO uses SysOperation framework. Here are a few links with more information about this framework:

  • DaLianAxNewbieJJP Profile Picture
    35 on at
    RE: How to get excel file url or getting excel from the batch job

    Hello Martin,

    I know you. You are very famous. I am not an English speaking country, so please forgive my English.

    First of all, I think D365FO or AX are developed in X++ language, so I think there is nothing wrong with it.

    Second, I want to express my opinion. Is there a problem with the code, but when I execute batch processing in the background, the file address is not found.

    Third, I really don’t know the existence of the SysOperation framework. Since AX2012, I have been using runbasebatch because it can provide dialog related information. Method, so please tell me how to get the temporary excel file that has been uploaded to the batch job thanks.

  • Martin Dráb Profile Picture
    231,933 Most Valuable Professional on at
    RE: How to get excel file url or getting excel from the batch job

    I'm assuming that you're taliking about D365FO, although you're asking in the AX forum.

    First of all, consider if you can't simply use data management, rather then developing your own solution for data import.

    If you can't, please tell us how you're working with fileUrl. It's difficult to find the bug in your code if you don't give us almost any information about the code.

    By the way, do you have any particular reason for using RunBaseBatch framework instead of the newer SysOperation framework? Note that many developers failed to implement serialization correctly when using RunBaseBatch framework, or even didn't realize that it's necessary. Maybe you've made the same mistake.

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,274 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,933 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans