Tuesday, November 6, 2018

X++ | d365FO - import and export data via XML (Part 1 - Export)

This post shows how to move data via XML. I found it very useful as ax consultants or developers often need to move the data (mostly parameters) from system to system.

Scenario
We would like to 'copy' parameters from dev system to test system. The tables and fields of both systems are identical. We just need to update (sync) the latest parameters on the test system.

Action
To show the example, I create a test table, then insert some data as below.














Write and run this code.
 class Example_Job_XMLexport  
 {      
   public static void main(Args _args)  
   {  
     const str filename          = 'TestTblParm.xml';  
     TextBuffer textBuffer     = new TextBuffer();  
     container tableCon          = [ 'TestTable'//,  
                                              //'Test2Table',  
                                              //'Test3Table',  
                                          ];  
     int            tableIdx,  
                       fieldIdx;  
     DictTable dictTable;  
     DictField dictField;  
     common   record;  
     BinData  binData;  
     str            fileContentStr;  
     void exportFieldValue(int _fieldId)  
     {  
       str fieldName = fieldId2Name(record.TableId, _fieldId);  
       str fieldValue;  
       ;  
       textBuffer.appendText('<field name="' + fieldName + '"><![CDATA[');  
       dictField = dictTable.fieldObject(_fieldId);  
       switch(dictField.baseType())  
       {  
         case Types::VarString:  
         case Types::String:  
           fieldValue = record.(_fieldId);  
           break;  
         case Types::Integer:  
         case Types::Int64:  
         case Types::Real:  
           fieldValue = strFmt('%1', record.(_fieldId));  
           break;  
         case Types::Date:  
           fieldValue = date2str(record.(_fieldId),123,1,1,1,1,1);  
           break;  
         case Types::Enum:  
           fieldValue = int2str(record.(_fieldId));  
           break;  
         case Types::Guid:  
           fieldValue = guid2str(record.(_fieldId));  
           break;  
         case Types::Container:  
           break;  
         case Types::UtcDateTime:  
           fieldValue = datetime2str(record.(_fieldId));  
           break;  
         default:  
           info(strFmt('Type %1 not exported', dictField.baseType()));  
           break;  
       }  
       binData = new BinData();  
       binData.setStrData(fieldValue);  
       textBuffer.appendText(binData.base64Encode());  
       textBuffer.appendText(']]></field>');  
     }  
     ;  
           // Start writing textBuffer  
     textBuffer.appendText('<data>');  
           // Loop through list of desired table  
     for(tableIdx = 1; tableIdx <= conLen(tableCon); tableIdx++)  
     {  
       dictTable = new DictTable(tableName2Id(conPeek(tableCon, tableIdx)));  
       record = dictTable.makeRecord();  
       while select record  
       {  
         textBuffer.appendText('<record table="'+ dictTable.name() +'">');  
         for(fieldIdx = 1; fieldIdx <= dictTable.fieldCnt(); fieldIdx++)  
         {  
           exportFieldValue(dictTable.fieldCnt2Id(fieldIdx));  
         }  
         textBuffer.appendText('</record>');  
       }  
     }  
           // End writing textBuffer  
     textBuffer.appendText('</data>');  
           // Set string  
     fileContentStr = textBuffer.getText();  
           // Export file  
     File::SendStringAsFileToUser(fileContentStr,  
                                         filename,  
                    System.Text.Encoding::UTF8,  
                                         classstr (FileUploadTemporaryStorageStrategy));  
     info('parameters exported');  
   }  
 }  

Then you get the result a XML file which contains the encoded content.

















In the next post, we will see how to import this XML file.

No comments:

Post a Comment