Monday, June 26, 2023

Code to export all vendors to XML file using X++ in D365

 public static void main(Args _args)

    {

        VendAccount vendAcc;

        XmlDocument doc;

        XmlElement nodeXml,Audit; //Main nodes.

        XmlElement nodeTable,BusinessUnit,AlternateId,VendorCustomFields; //Sub nodes

        XmlElement  nodeAccount,name,name2,StreetAddress1,

                    City,State,PostalCode,Country,ContactName,

                    ContactPhone,ContactFax,ContactEmail,BusinessUnitID,BusinessUnitName,

                    Status,PaymentMethod,PaymentTerm,AlternateType,AlternateIDNbr,

                    CFNbr,Value,Description,VendorCount;

        XmlElement nodeName;

        int x = 0;      

        VendTable    vendTable;

        DirPartyLocation    dirpartylocation;

        DirPartyTable    dirPartyTable,dir;

        LogisticsPostalAddress  logisticsPostalAddress,logisticsPostalAddressloc;

        LogisticsLocation    logisticsLocation;

        LogisticsElectronicAddress  logisticsElectronicAddress,logisticsFax,logisticsEmail;

        CompanyInfo company;

        #define.filename(@'C:\Temp\accountsloc.xml')  //creates a file in Temp folder of c drive.

        doc = XmlDocument::newBlank();

        nodeXml = doc.createElement('xml');     //creates a new node. Starting point = xml

        doc.appendChild(nodeXml);

        while select vendTable

             where vendTable.Blocked == CustVendorBlocked::No

        {

            vendAcc = vendTable.AccountNum;

            select dirPartyTable

                where dirPartyTable.RecId == vendTable.Party;

            select logisticsPostalAddress

                 where logisticsPostalAddress.Location == dirPartyTable.PrimaryAddressLocation;

           select logisticsLocation

                where logisticsLocation.RecId == dirPartyTable.PrimaryAddressLocation;

            x++; //To get count of vendor.

            company = CompanyInfo::find();

            dir = DirPartyTable::findRec(vendtable.Party);

            logisticsElectronicAddress = LogisticsElectronicAddress::findRecId(dir.PrimaryContactPhone,true);


            logisticsFax   =   LogisticsElectronicAddress::findRecId(dir.PrimaryContactFax);

            logisticsEmail = LogisticsElectronicAddress::findRecId(dir.PrimaryContactEmail);

            nodeTable = doc.createElement("vendor");   // create a subnmode to xml node as Vendor inside xml


            nodeXml.appendChild(nodeTable);

            nodeAccount = doc.createElement("CustomersVendorNbr");

            nodeAccount.appendChild(doc.createTextNode(vendTable.AccountNum));


            nodeTable.appendChild(nodeAccount);

            name = doc.createElement("Name");

            name.appendChild(doc.createTextNode(dirPartyTable.Name));

            nodeTable.appendChild(name);


            StreetAddress1 = doc.createElement("StreetAddress1");

            StreetAddress1.appendChild(doc.createTextNode(logisticsPostalAddress.Street));

            nodeTable.appendChild(StreetAddress1);


            City = doc.createElement("City");

            City.appendChild(doc.createTextNode(logisticsPostalAddress.City));

            nodeTable.appendChild(City);


            State = doc.createElement("State-Region");

            State.appendChild(doc.createTextNode(logisticsPostalAddress.state));

            nodeTable.appendChild(State);


            PostalCode = doc.createElement("PostalCode");

            PostalCode.appendChild(doc.createTextNode(logisticsPostalAddress.ZipCode));

            nodeTable.appendChild(PostalCode);


            Country = doc.createElement("Country");

            Country.appendChild(doc.createTextNode(logisticsPostalAddress.County));

            nodeTable.appendChild(Country);


            ContactName = doc.createElement("ContactName");

            ContactName.appendChild(doc.createTextNode(logisticsElectronicAddress.Description)); 

            nodeTable.appendChild(ContactName);


            ContactPhone = doc.createElement("ContactPhone");

            ContactPhone.appendChild(doc.createTextNode(logisticsElectronicAddress.locator)); 

            nodeTable.appendChild(ContactPhone);


            ContactFax = doc.createElement("ContactFax");

            ContactFax.appendChild(doc.createTextNode(logisticsFax.locator));

            nodeTable.appendChild(ContactFax);


            ContactEmail = doc.createElement("ContactEmail");

            ContactEmail.appendChild(doc.createTextNode(logisticsEmail.locator));

            nodeTable.appendChild(ContactEmail); 


            BusinessUnit = doc.createElement("BusinessUnit"); // create a node as business unit inside of vendor.

            nodeTable.appendChild(BusinessUnit);

            BusinessUnitID = doc.createElement("BusinessUnitID"); // To get Company Id eg:usmf 

            BusinessUnitID.appendChild(doc.createTextNode(vendTable.DataAreaId));

            BusinessUnit.appendChild(BusinessUnitID);


            BusinessUnitName = doc.createElement("BusinessUnitName"); // To get Company name.

            BusinessUnitName.appendChild(doc.createTextNode(company.Name));

            BusinessUnit.appendChild(BusinessUnitName);


            PaymentMethod = doc.createElement("PaymentMethod");

            PaymentMethod.appendChild(doc.createTextNode(vendTable.PaymMode));

            BusinessUnit.appendChild(PaymentMethod);


            PaymentTerm = doc.createElement("PaymentTerm");

            PaymentTerm.appendChild(doc.createTextNode(vendTable.PaymTermId));

            BusinessUnit.appendChild(PaymentTerm);


            AlternateID = doc.createElement("AlternateID"); //It is subnode of BusinessUnit. it is inside of Business unit as alternateId.

            BusinessUnit.appendChild(AlternateID);

            AlternateType = doc.createElement("AlternateType");

            AlternateType.appendChild(doc.createTextNode("ALT"));

            AlternateID.appendChild(AlternateType);

            AlternateIDNbr = doc.createElement("AlternateIDNbr"); // To get tax federal tax amount

            AlternateIDNbr.appendChild(doc.createTextNode(vendTable.Tax1099RegNum));

            AlternateID.appendChild(AlternateIDNbr);


            VendorCustomFields = doc.createElement("VendorCustomFields"); //It is subnode of BusinessUnit.it is inside of Business unit as VendorCustomFields .

            BusinessUnit.appendChild(VendorCustomFields);

            boolean values = false;

                         //loop to get address of vendor if purpose is Remit to only.

            while select dirpartylocation

                where dirpartylocation.Party == vendtable.Party

                && dirpartylocation.PostalAddressRoles == enum2Str(LogisticsLocationRoleType::RemitTo)

            {

                values = true;

                    select logisticsPostalAddressloc

                         where logisticsPostalAddressloc.Location == dirpartylocation.Location;

                    CFNbr = doc.createElement("CFNbr");

                    CFNbr.appendChild(doc.createTextNode("1"));

                    VendorCustomFields.appendChild(CFNbr);

                    Value = doc.createElement("Value");

                    Value.appendChild(doc.createTextNode(logisticsPostalAddressloc.Address));

                    VendorCustomFields.appendChild(Value);

                    Description = doc.createElement("Description");

                    Description.appendChild(doc.createTextNode(logisticsPostalAddressloc.Address));

                    VendorCustomFields.appendChild(Description);

            }

            if(!values)

            {

                CFNbr = doc.createElement("CFNbr");

                CFNbr.appendChild(doc.createTextNode("1"));

                VendorCustomFields.appendChild(CFNbr);


                Value = doc.createElement("Value");

                VendorCustomFields.appendChild(Value);


                Description = doc.createElement("Description");

                VendorCustomFields.appendChild(Description);

            }

                }

        Audit = doc.createElement("Audit");

        nodeXml.appendChild(Audit);

        VendorCount = doc.createElement("VendorCount");

        VendorCount.appendChild(doc.createTextNode(int2Str(x))); //To get the count of loop. vendors

        Audit.appendChild(VendorCount);

        doc.save(#filename);

        Info(strFmt("file created"));

    }


Output :



No comments:

Post a Comment