Maxime
Maxime

Reputation: 858

Creating a file with Acumatica Framework

How do you generate a file on Acumatica ? I didnt find the process in the trainings nor the framework

I need to add a button like the "export to excel" but with xml for example but a simple button on a screen generating my xml would be enough

Thanks

Upvotes: 0

Views: 338

Answers (1)

Hugues Beauséjour
Hugues Beauséjour

Reputation: 8278

Follow standard procedure to add the button to your grid:

Add button to grid in Acumatica

Adding button on newly created tab

In your button event handler:

[PXButton]
[PXUIField(DisplayName = "Export XML")]
public virtual IEnumerable exportXML(PXAdapter adapter)
{
   // Use your favorite library to create XML file
   XmlDocument xmlDoc = new XmlDocument();
   xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null));

   // Create root node of XML file
   XmlNode rootNode = xmlDoc.CreateElement("Grid Data");

   // Iterate rows of the DataView
   foreach (DAC dacRecord in GridDataView.Select())
   {      
      // Create an XML Element to represent the DAC row
      XMLNode xmlDACRecord = xmlDoc.CreateElement("DAC Record");

      // Add desired DAC fields as child XML Elements of the DAC row XML element
      XMLNode xmlDACField1 = xmlDoc.CreateElement("DAC Field 1");
      xmlDACField1.AppendChild(xmlDoc.CreateTextNode(dacRecord.Field1.ToString()));
      xmlDACRecord.AppendChild(xmlDACField1);

      // Adding XML DAC Record to XML root node
      rootNode.AppendChild(xmlDACRecord);
   }

   // Adding XML root node to XML document
   xmlDoc.AppendChild(rootNode);

   // Redirect browser to XML file created in memory on server
   throw new PXRedirectToFileException(new PX.SM.FileInfo(Guid.NewGuid(),
                                                          "filename.xml",
                                                          null,
                                                          System.Text.Encoding.UTF8.GetBytes(xmlDoc.OuterXml)), 
                                       true);
   return adapter.Get();
}

Upvotes: 2

Related Questions