Wednesday, June 5, 2024

Read value from XML (Attribute based) in D365FO

     public void readXML_Test(str _invoiceId)

    {

        str xmlData = @'<?xml version="1.0" encoding="UTF-8"?>

<cfdi:Comprobante Moneda="MXN" xsi:schemaLocation="<URL>" Version="4.0" Serie="MI" Folio="22" Fecha="2024-05-07T15:55:22" NoCertificado="01101111100504511177" Certificado="Q1MDkwNzcwDQYJKo/mgAwIBAgIUMDAwMD5on" CondicionesDePago="Neto 30 Dias" Total="1160.00" TipoDeComprobante="I" MetodoPago="PPP" FormaPago="99" LugarExpedicion="66269" Exportacion="01" SubTotal="1000.00" Sello="Rpf" xmlns:cfdi="<URL>" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <cfdi:Emisor Rfc="TTTX6" Nombre="TEST SALES MEXICO" RegimenFiscal="601"/>

    <cfdi:Receptor Rfc="TT10427D90" Nombre="TEST INTERNATIONAL" UsoCFDI="G01" DomicilioFiscalReceptor="6310" RegimenFiscalReceptor="601"/>

    <cfdi:Conceptos>

        <cfdi:Concepto ClaveProdServ="10111300" NoIdentificacion="10111300" Cantidad="1" ClaveUnidad="ACT" Unidad="un" Descripcion="test" ValorUnitario="1000.000000" Importe="1000" ObjetoImp="02">

            <cfdi:Impuestos>

                <cfdi:Traslados>

                    <cfdi:Traslado Base="1000.00" Impuesto="002" TipoFactor="Tasa" TasaOCuota="0.160000" Importe="160.00"/>

                </cfdi:Traslados>

            </cfdi:Impuestos>

        </cfdi:Concepto>

 <cfdi:Concepto ClaveProdServ="10111300" NoIdentificacion="10111300" Cantidad="1" ClaveUnidad="ACT" Unidad="un" Descripcion="test" ValorUnitario="1000.000000" Importe="1000" ObjetoImp="02">

            <cfdi:Impuestos>

                <cfdi:Traslados>

                    <cfdi:Traslado Base="1000.00" Impuesto="002" TipoFactor="Tasa" TasaOCuota="0.160000" Importe="160.00"/>

                </cfdi:Traslados>

            </cfdi:Impuestos>

        </cfdi:Concepto>

    </cfdi:Conceptos>

    <cfdi:Impuestos TotalImpuestosTrasladados="160.00">

        <cfdi:Traslados>

            <cfdi:Traslado Base="1000.00" Impuesto="002" TipoFactor="Tasa" TasaOCuota="0.160000" Importe="160.00"/>

        </cfdi:Traslados>

    </cfdi:Impuestos>

    <cfdi:Complemento>

        <tfd:TimbreFiscalDigital Version="1.1" UUID="27230B06-966F-4F0B-8111-3F57EEBF4841" FechaTimbrado="2024-05-07T15:56:38" RfcProvCertif="INT020124V62" SelloCFD="Rpf2O==" xsi:schemaLocation="<>URL3" xmlns:tfd="<URL1>"/>

    </cfdi:Complemento>

</cfdi:Comprobante>';


        XmlDocument xmlDocument;

        XmlNodeList xmlRecords, xmlFields, xmlFields1, xmlFields2, xmlFields3;

        XmlNode xmlRecord, xmlField, xmlField1, xmlField2, xmlField3;

        XMLParseError xmlError;

        XmlElement xmlParentRecord;

        xmlDocument = new XmlDocument();

        xmlDocument.loadXml(xmlData);


        xmlError = xmlDocument.parseError();


        Map         mapHeader = new Map(Types::String, Types::Container);

        Map         mapParentNode = new Map(Types::String, Types::Container);

        Map         mapSummary = new Map(Types::String, Types::Container);

        Map         mapItemInfo = new Map(Types::String, Types::Container);


        str     einvoiceId = _invoiceId;

        xmlParentRecord = xmlDocument.root();

        var name = xmlParentRecord.name();

        xmlRecords  = xmlDocument.root().childNodes();

        xmlRecord   = xmlRecords.nextNode();

        XmlNamedNodeMap mapValue;

        XmlNode     curNode;


        mapValue = xmlParentRecord.attributes();

        

        if(!mapParentNode.exists(strFmt("ParentNode%1", einvoiceId)))

        {

            mapParentNode.insert(strFmt("ParentNode%1", einvoiceId), [mapValue.getNamedItem('CondicionesDePago').nodeValue(),mapValue.getNamedItem('Moneda').nodeValue(), mapValue.getNamedItem('TipoDeComprobante').nodeValue()]);

        }


        while (xmlRecord)

        {

            mapValue = null;

            switch(xmlRecord.name())

            {

                case "cfdi:Receptor" : //XML without any sub XML tags

                    mapValue = xmlRecord.attributes();

                    if(!mapHeader.exists(strFmt("Header%1%2", einvoiceId,xmlRecord.name())))

                    {

                        mapHeader.insert(strFmt("Header%1%2", einvoiceId,xmlRecord.name()), [mapValue.getNamedItem('UsoCFDI').nodeValue(),mapValue.getNamedItem('Nombre').nodeValue(), mapValue.getNamedItem('DomicilioFiscalReceptor').nodeValue()]);

                    }

                    break;


                case "cfdi:Emisor" : //XML without any sub XML tags

                    mapValue = xmlRecord.attributes();

                    if(!mapHeader.exists(strFmt("Header%1%2", einvoiceId,xmlRecord.name())))

                    {

                        mapHeader.insert(strFmt("Header%1%2", einvoiceId,xmlRecord.name()), [mapValue.getNamedItem('RegimenFiscal').nodeValue()]);

                    }

                    break;


                case "cfdi:Conceptos" : //XML with sub XML tags

                    xmlFields = xmlRecord.childNodes();

                    xmlField = xmlFields.nextNode();

                    int counter;

                    while (xmlField)

                    {

                        counter++;

                        anytype     claveProdServ,claveUnidad,netAmountImporte,description, base,impuesto,tipoFactor,TasaOcuota,actualSalesTaxImporte,descuento;

                        mapValue = xmlField.attributes();

                        claveProdServ = mapValue.getNamedItem('ClaveProdServ').nodeValue();

                        claveUnidad = mapValue.getNamedItem('ClaveUnidad').nodeValue();

                        netAmountImporte = mapValue.getNamedItem('Importe').nodeValue();

                        description = mapValue.getNamedItem('Descripcion').nodeValue();

                        if(mapValue.getNamedItem('Descuento'))

                        {

                            descuento = mapValue.getNamedItem('Descuento').nodeValue();

                        }

                        xmlFields1 = xmlField.childNodes();

                        xmlField1 =xmlFields1.nextNode();

                        while (xmlField1)

                        {

                            xmlFields2 = xmlField1.childNodes();

                            xmlField2 =xmlFields2.nextNode();


                            while (xmlField2)

                            {

                                xmlFields3 = xmlField2.childNodes();

                                xmlField3 =xmlFields3.nextNode();


                                while (xmlField3)

                                {

                                    mapValue = xmlField3.attributes();

                                    base = mapValue.getNamedItem('Base').nodeValue();

                                    impuesto = mapValue.getNamedItem('Impuesto').nodeValue();

                                    tipoFactor = mapValue.getNamedItem('TipoFactor').nodeValue();

                                    TasaOCuota = mapValue.getNamedItem('TasaOCuota').nodeValue();

                                    actualSalesTaxImporte = mapValue.getNamedItem('Importe').nodeValue();

                                    xmlField3 = xmlFields3.nextNode();

                                }

                                xmlField2 = xmlFields2.nextNode();

                            }

                            xmlField1 = xmlFields1.nextNode();

                        }


                        if(!mapItemInfo.exists(strFmt("Lines%1%2", einvoiceId,counter)))

                        {

                            mapItemInfo.insert(strFmt("Lines%1%2", einvoiceId,counter), [claveProdServ,claveUnidad,netAmountImporte,description, base,impuesto,tipoFactor,TasaOcuota,actualSalesTaxImporte,descuento]);

                        }

                        xmlField =xmlFields.nextNode();

                    }

                    break;

            }

            xmlRecord = xmlRecords.nextNode();

        }


        //To read values

        //if(mapItemInfo.exists(strFmt("Lines%1%2",InvoiceId,lineCounter)))

        //{

        //    container   itemInfoLines = mapItemInfo.lookup(strFmt("Lines%1%2",eInvoiceJourMapping.invoiceId(),lineCounter));

        //    conPeek(itemInfoLines,1);

        //    conPeek(itemInfoLines,2);

        //    conPeek(itemInfoLines,3);

        //    conPeek(itemInfoLines,4);

        //}

    }

Using SysOperationSandbox::callStaticMethod Sample in D365FO X++

 SysOperationSandbox::callStaticMethod(classnum(Classnum(<Class name>)), staticMethodStr(<Class name>, <Class static method n...