Have you searched the net for how to store documents in an AX table? I did, a lot.. When I couldn’t find it, I had to do it myself 😉 Here is a short description on how to store, download and edit documents from AX in Office (or any..) program.
First you need to create a table with a field of the type Container. Then add the Extended data type BinData to that field. My example table looks like this ;
1 2 3 4 5 | FO_DocuTest: int FileNum str FileName str FileType // doc, xls or whatever container FileData // BinData as extended. |
First, here is a short table method on how to load from disk.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | // load from disk void load(str _path) { BinData binData = new BinData(); ; if (binData.loadFile(_path + "\\" + this.FileName + "." + this.FileType)) { //only works if file not locked. this.FileData = binData.getData(); } else throw error(strfmt("@SYS54217",_path)); } |
Second, a short table method on how to save back to disk.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | // save to disk void save(str _path) { BinData binData = new BinData(); ; binData.setData( this.FileData ); if (!binData.saveFile(_path + "\\" + this.FileName + "." + this.FileType)) { //only works if file not locked throw error(strfmt("@SYS54217",_path)); } } |
Then you have these two, you only need to know how to start Word/Excel. I use Word in this example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | void clicked() // click method in a form. { COM app,documents,doc; ; super(); FO_DocuTest.save(mTempDirectory); // save blob to disk. app = new com("Word.Application"); documents = app.Documents(); doc = documents.open(mTempDirectory + "\\" + FO_DocuTest.FileName + "." + FO_DocuTest.FileType); doc.activate(); app.visible(true); } |
And finally, a method on how to upload back to table again.
1 2 3 4 5 6 7 8 9 10 11 | void clicked() // click method in a form. { super(); FO_DocuTest.load(mTempDirectory); // load back to blob FO_DocuTest.update(); // remove temp file again. WinApi::deleteFile(mTempDirectory + "\\" + FO_DocuTest.FileName + "." + FO_DocuTest.FileType); } |
Voila! Not more than that… After searching around on the net, I found it strange that no one presented this, but now you have it!
Last 5 posts in Dynamics AX
- How to call a web API from Dynamics 365 FO X++ - December 1st, 2020
- Cannot add or remove static keyword for this method - May 30th, 2016
- Sales line number not unique - May 27th, 2016
- DMF - DMF Service is unavailable - November 7th, 2012
- DMF - Error occured while doing bulkcopy from temp table to entity table - November 6th, 2012
Hi Henrik,
Good article, but have you looked into the standard document handling of AX?
Cheers,
Palle Agermark
Thanks. Yes, It’s there I got some of the ideas from, but I wanted an isolated solution to demonstrate. DocuRef, DocuTable etc contains so much more and would make a much bigger article. I wanted to pick out only the pure basics needed to explain this.
/Henrik
Thanks — just what I was looking for!