Exporting and Importing objects with C/AL in NAV 2009 SP1

If you're familiar with the default TIF tool that comes out-of-the box from Microsoft, and you've been working with it quite some time .. you definitely started to wonder how you can make things faster or easier … how you can automate some "actions", like exporting and importing objects. Exactly this we (at my office) were investigating on how we could automate this.

A while ago, we were looking for a good solution for "making the TIF Tool a little bit more easy". We were looking for a way to export and import objects automagically. And apparently, there are some new possibilities in C/SIDE to do that.

There are two new instructions: IMPORTOBJECTS and EXPORTOBJECTS. You can find them in the symbol menu in SYSTEM/Object/…:

This feature is really undocumented .. In the way that I really can't find any documentation whatsoever of it. So please don't mind the mistakes or misinterpretations that might come up in this article… .

Exporting objects

The EXPORTOBJECTS-statement has got three parameters:

  • File name: this is the name of the file (duh…)
  • ObjectRecord: this is a record-variable of the Object-table.
  • Format: this in an integer where you can specify in which format you would like to export the object. These are the choices:
    • 0: txt-file
    • 1: xml-file

So, no fob file! If you try another integer (e.g. "2"), you get an error that explains that there are only two supported formats:

As you can see, it's very straight forward. If you want to export all modified objects, you could write something like:

recObject.SETRANGE(Modified,TRUE);
EXPORTOBJECTS('C:\==WORKINGFOLDER\Temp\Objects_Format_0.txt',recObject,0); //txt
EXPORTOBJECTS('C:\==WORKINGFOLDER\Temp\Objects_Format_1.xml',recObject,1); //xml

Easy, isn't it? With this code, I'm exporting to both text and xml. Does it need more explanation? I think not Smile.

Importing Objects

For importing objects, you need the IMPORTOBJECTS-statement, which has got only two parameters:

  • File Name: the name of the file (yet again … duh …)
  • Format: an integer which can have the same two values:
    • 0: to import a textfile
    • 1: to import a xml-file

Importing a file is nothing more then calling the statement:

IMPORTOBJECTS('c:\MyFile.txt',0); //import a textfile
IMPORTOBJECTS('c:\MyFile.xml',1); //import a xml-file

Why isn't my table exporting to xml?

When you export to xml, you notice that not all objects are exported. Only Forms and Reports are exported to XML. When you try it by hand (File/Export), you actually notice the same thing. Try to export a table to xml … you'll notice it just creates an empty xml-file.

You might wonder why this is. Well, obviously (and this is just a guess) the export-to-xml option was primarily developed to support the transformation (FORM and REPORT transformation).

Why would this be useful?

I have seen a number of threads on Mibuso and DynamicsUser where people want to be able to import/export objects, so I know there are some of you that will be quite glad :-). If I think of it, I can imagine people see some possibilities in some kind of "version management" system, to keep track of the changes per object. Furthermore, there is may be a possibility to "Fetch" objects from a customer and import it in a dev-environment … or the other way around?

The combination of these statements with some possibilities you have in the object table (changing version list, modified-flag,…), or web services in NAV 2009 (sending/receiving objects to/from a customer or other), or… can lead to creative development tools if you ask me :-). Ideas are always welcome! Post them as a comment Smile.

Are there other things to think about?

Well, the only thing I could think of (at the moment) is the fact that these statements don't work in the RTC (RoleTailored Client). You'll get this error message:

Does it matter? As long as the development environment is in the Classic, I don't think it will matter. It

only matters when you want to combine it with Web Services. That runs on the service tier, which means that importing or exporting the objects won't work through web services process. You'll need to include a NAS to do that … and that makes things a little bit more complicated.    

That was it. Enjoy!

   

   

Comment List
Related
Recommended