Thursday, February 22, 2018

X++ code to export data from AX to XML

Using Class:

Class Declaration:
class CustomerExportXML
{
}

Main:
public static void main(Args _args)
{
    XmlDocument     doc;
    XmlElement        nodexml;
    XmlElement        nodeTable;
    XmlElement        nodeAccountNum;
    XmlElement        nodeCustGroupId;
    XmlElement        nodeName;
    CustTable            custTable;
    DirPartyTable     dirPartyTable;
    DirParty              dirParty;
    MethodInfo         methodInfo;
    #define.filename(@'D:\Temp\TestXML.xml')

    doc     = XmlDocument::newBlank();
    nodexml = doc.createElement('xml');
    doc.appendChild(nodexml);

    while select party, AccountNum, CustGroup from custTable join dirPartyTable
        //where custTable.party == DirPartyTable.RecId  commented line
    {
        nodeTable = doc.createElement(tableStr(CustTable));
        nodeTable.setAttribute(fieldStr(CustTable, RecId),int642str(custTable.RecId));
        nodexml.appendChild(nodeTable);

        nodeAccountNum = doc.createElement(fieldStr(CustTable, AccountNum));
        nodeAccountNum.appendChild(doc.createTextNode(custTable.AccountNum));
        nodeTable.appendChild(nodeAccountNum);

        nodeCustGroupId =  doc.createElement(fieldStr(CustTable, CustGroup));
        nodeCustGroupId.appendChild(doc.createTextNode(custTable.CustGroup));
        nodeTable.appendChild(nodeCustGroupId);

        /*nodeName = doc.createElement(fieldStr(dirPartyTable, Name));
        nodeName.appendChild(doc.createTextNode(custTable.name()));
        nodeTable.appendChild(nodeName);*/

        nodeName = doc.createElement("Name");
        nodeName.appendChild(doc.createTextNode(CustTable.name()));
        nodeTable.appendChild(nodeName);
    }
    doc.save(#filename);
    info(strFmt("File %1 created.", #filename));
}


Using Job:

static void ExportXML(Args _args)
{
    XmlDocument         xmlDoc;
    XmlElement          xmlRoot;
    XmlElement          xmlField;
    XmlElement          xmlRecord, xmlRecord1, xmlRecord2;
    XmlWriter           xmlWriter;
    CustTable           custTable;
    DictTable           dictTable = new DictTable(tableNum(CustTable));
    DictField           dictField;
    int                 i, fieldid, fieldid_1;
    str                 value;
    FileIOPermission    _perm;
    AccountNum          _accountNum;
    ;

    xmlDoc      = XmlDocument::newBlank();
    xmlRoot     = xmlDoc.createElement("AccountNum");
    _accountNum = "DE-001";


   while select AccountNum from  custTable
        where custTable.AccountNum == _accountNum
    {
        xmlRecord   = xmlDoc.createElement("custtable");
        for(i=1; i<=3; i++)
        {
            switch(i)
            {
                case 1:
                    fieldid = fieldname2id(custTable.TableId, "AccountNum");
                    break;

                case 2:
                    fieldid = fieldName2id(custTable.TableId, "CustGroup");
                    break;

                case 3:
                    fieldid = fieldName2id(custTable.TableId, "DIPL_SurName");
                    break;

                case 4:
                    fieldid = fieldName2id(custTable.TableId, "CustItemGroupId");
                    break;
            }
            dictField   = dictTable.fieldObject(fieldid);
            xmlField    = xmlDoc.createElement(dictField.name());
            value       = custTable.(fieldid);
            xmlField.innerText(value);
            xmlRecord.appendChild(xmlField);
            Commented Line start/*dictField   = dictTable.fieldObject(fieldId);
            xmlField    = xmlDoc.createElement(dictField.name());
            switch (dictField.baseType())
            {
                case Types::Int64 :
                    value = int642str(custTable.(fieldId));
                    break;

                case Types::Integer :
                    value = int2str(custTable.(fieldId));
                    break;

                default :
                    value = custTable.(fieldId);
                    break;
            }
            xmlField.innerText(value);
            xmlRecord.appendChild(xmlField);

            if(value != "" && fieldId == fieldname2id(custTable.TableId,"AccountNum"))
            {
                fieldId_1   = fieldname2id(custTable.TableId,"AccountNum");
                dictField   = dictTable.fieldObject(fieldId_1);
                xmlField    = xmlDoc.createElement(dictField.name());
                value       = custTable.(fieldId_1);
                xmlField.innerText(value);
                xmlRecord.appendChild(xmlField);

            }
            else if((value == "" && fieldId == fieldname2id(custTable.TableId,"AccountNum")))
            {
                fieldId_1   = fieldname2id(custTable.TableId,"AccountNum");
                dictField   = dictTable.fieldObject(fieldId_1);
                xmlField    = xmlDoc.createElement(dictField.name());
                value       = custTable.(fieldId_1);
                xmlRecord.appendChild(xmlField);
            }*/ Commented Line Ends
            xmlRoot.appendChild(xmlRecord);
        }
    }
    xmlDoc.appendChild(xmlRoot);
    info(xmlDoc.toString());
    xmlWriter = XmlWriter::newFile(@'D:\Temp\TestXML.xml');
    xmlDoc.writeTo(xmlWriter);
}

Refer below link for better understanding

Exporting data to XML