Microsoft_MVP_banner

AX 2012: Create sales order with delivery address from an XML file

Code snippet to create a sales order from an XML file. private void createSOFromFile() {     AxdSalesOrder   salesOrder;     AifEntityKey    key;         Filename        fileName;     RecId           salesTableRecId;     SalesTable      salesTable;         XmlDocument     xmlDoc;         salesOrder  = new AxdSalesOrder();     fileName    = @”C:TempSalesOrder.XML”;         xmlDoc = XmlDocument::newFile(fileName);         key = salesOrder.create(xmlDoc.xml(),                 new AifEndPointActionPolicyInfo(),                new AifConstraintList());     salesTableRecId = key.parmRecId();         info(strFmt(“Sales order created: %1”, SalesTable::findRecId(salesTableRecId).SalesId)); } Sample XML file <?xml version=”1.0″ encoding=”UTF-8″?> <SalesOrder xmlns:n=”http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder”>    <SalesTable class=”entity”>       <CustAccount>C000002</CustAccount>       <InventLocationId>EMU</InventLocationId>       <CustomerRef>Cash on Delivery (COD)</CustomerRef>       <PurchOrderFormNum>4669286216,1021</PurchOrderFormNum>       <ReceiptDateRequested>2011-11-11</ReceiptDateRequested>       <DeliveryName>team03qa@gmail.com</DeliveryName>       <TableDlvAddr class=”entity”>          <City>southbank</City>          <CountryRegionId>AUS</CountryRegionId>          <LocationName>delivery address</LocationName>          <State>VIC</State>          <Street>13075 MANCHESTER RD STE</Street>          <ZipCode>3006</ZipCode>       </TableDlvAddr>       <SalesLine class=”entity”>          <ItemId>CANO</ItemId>          <SalesQty>20</SalesQty>          <SalesPrice>82.25</SalesPrice>          <SalesUnit>ltr</SalesUnit>          <LineAmount>1</LineAmount>          <LineNum>8966003144</LineNum>       </SalesLine>       <SalesLine class=”entity”>          <ItemId>CANO</ItemId>          <SalesPrice>9.9</SalesPrice>          <SalesQty>1</SalesQty>          <SalesUnit>ltr</SalesUnit>          <LineAmount>9.9</LineAmount>          <LineNum>8966003208</LineNum>       </SalesLine>    </SalesTable> </SalesOrder>  How does it work AxdSalesOrder class uses AifEntity classes to create sales order, these classes internally map the datasources of an AOT query AxdSalesOrder with the element tags in XML with attribute class = “entity”. For example; for delivery address, the XML node name is TableDlvAddr which is the datasource name in AxdSalesOrder query. Similarly, if you want to add notes or attach documents with sales order you can add DocuRefHeader node in XML file.

AX 2012: Reading XML Nodes under specific tag through X++

XML to read X++ Code  For above XML structure, I will read all nodes exist within each InstructorAPIModel node. The getElementsByTagName() method of XmlDocument class can be used which returns XmlNodeList which can then be iterated to get node’s values.   XmlDocument doc = new XmlDocument(); XmlNodeList apiModelList; XmlNodeListIterator iterator; XmlElement apiModel; XmlElement firstName; XmlElement lastName; doc.loadXml(_xmlMsg); apiModelList = doc.getElementsByTagName(‘InstructorApiModel’); iterator= new XmlNodeListIterator(apiModelList); while (iterator.moreValues()) { apiModel = iterator.value(); firstName = apiModel.getNamedElement(‘FirstName’); if (firstName) { info(strFmt(“First Name: %1”, firstName.text())); } lastName = apiModel.getNamedElement(‘LastName’); if (lastName) { info(strFmt(“Last Name: %1”, lastName.text())); } iterator.nextValue(); }

AX 2012: Reading XML Nodes through X++

XML to read X++ Code XmlDocument     doc; XmlNodeList     xmlScriptList; XmlNodeList     xmlResponseList; XmlNodeList     xmlTemplateList; XmlElement      nodeScript; XmlElement      nodeResponse; XmlElement      nodeTemplate; XMLParseError   xmlError; str xmlMsg; int i, j;     ;     // Create the XML Document doc = new XmlDocument(); doc.loadXml(xmlMsg); // Verify XML Document Structure xmlError  = doc.parseError(); if(xmlError && xmlError.errorCode() != 0) {     throw error(strFmt(“XML Error: %1”, xmlError.reason())); } // Get the root element and its child nodes nodeScript = doc.getNamedElement(“ns2:return”); xmlScriptList = nodeScript.childNodes(); for(i=0; i < xmlScriptList.length(); i++) {     nodeResponse = xmlScriptList.item(i);     xmlResponseList = nodeResponse.childNodes();     for (j=0; j < xmlResponseList.length(); j++)     {         nodeTemplate = xmlResponseList.item(j);         xmlTemplateList = nodeTemplate.childNodes();         if (nodeTemplate.selectSingleNode(‘id’))         {            info(strFmt(“ID: %1”, nodeTemplate.getNamedElement(“id”).text()));         }                      if (nodeTemplate.selectSingleNode(‘messageTemplateName’))         {            info(strFmt(“messageTemplateName: %1”, nodeTemplate.getNamedElement(“messageTemplateName”).text()));         }     } }

FaisalFareed@2025. All rights reserved

Design by T3chDesigns