0
votes

I am trying to generate a text file on an Action Button, with contents of a dataview created in a Graph Class and Save the file in my Local Drive. But i am unable to do it.

Please help me with the file generation...Thanks

I AM USING Acumatica Version 2019R2 (v 19.203.0042)

MY CODE GOES HERE...

public PXSelect<MayBankGIRO> Document; //this is my dataview
public PXAction<MayBankGiroFilter> createTextFile;
        [PXUIField(DisplayName = "Create Text File")]
        [PXButton()]
        public virtual IEnumerable CreateTextFile(PXAdapter adapter)
        {
            string filepath = "C:\\Subhashish Dawn";
            System.IO.StreamWriter sw = new System.IO.StreamWriter(filepath);
            MayBankGIRO giroObject = this.Document.Current;
            List<object> myListObject = new List<object> { };
            FixedLengthFile flatFile = new FixedLengthFile();
            foreach (MayBankGIRO dacRecord in this.Document.Select())
            {
                if (giroObject.ReordType == "00")
                {
                    myListObject.Add(dacRecord.ReordType + "|" + dacRecord.CorporateID + "|" + dacRecord.ClientBatchID + "|");
                }
                else
                {
                    myListObject.Add(dacRecord.ReordType + "|" + dacRecord.CorporateID + "|" + dacRecord.ClientBatchID + "|" + dacRecord.Country + "|");
                    string data = dacRecord.ReordType;

                }

                this.Document.Update(dacRecord);
            }
            flatFile.WriteToFile(myListObject, sw); 

            sw.Flush();
            sw.FlushAsync();

            string path = "DAWN" + ".txt";
            PX.SM.FileInfo file = new PX.SM.FileInfo(Guid.NewGuid(), path, null, System.Text.Encoding.UTF8.GetBytes(**path**)); // what shall i substitite in place of  **path**
            throw new PXRedirectToFileException(file, true);
}
``````````````````````````````````````````````````````
Can anyone please specify what changes in have to make in the above code.


3

3 Answers

0
votes

Assuming your Document object is the view you need and its Select() method returns all the data records you need inside your file, this should work:

// We need at least these, listing them for reference
using PX.Data;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;

// This is your dataview
public PXSelect<MayBankGIRO> Document; 

// Your action delegate
public PXAction<MayBankGiroFilter> createTextFile;

[PXUIField(DisplayName = "Create Text File")]
[PXButton()]
public virtual IEnumerable CreateTextFile(PXAdapter adapter)
{
    // You can use this method to print debug information for your customizations
    // Just remove when you are done testing
    PXTrace.WriteInformation("Generating records");

    // We will build the content as a string list first
    List<string> myList = new List<string> { };

    // If the value of 'ReordType' can change for each record, you don't need this
    MayBankGIRO giroObject = this.Document.Current;

    foreach (MayBankGIRO dacRecord in this.Document.Select())
    {
        // Does 'ReordType' change for each record?
        // if it does you may need to use 'dacRecord.ReordType' in this if instead
        if (giroObject.ReordType == "00")
        {
            // This only works if all these members are strings or can be cast to strings
            myList.Add(dacRecord.ReordType + "|" + dacRecord.CorporateID + "|" + dacRecord.ClientBatchID + "|");
        }
        else
        {
            // This only works if all these members are strings or can be cast to strings
            myList.Add(dacRecord.ReordType + "|" + dacRecord.CorporateID + "|" + dacRecord.ClientBatchID + "|" + dacRecord.Country + "|");
        }
    }

    PXTrace.WriteInformation("Generating file");

    // Set the name
    string filename = "DAWN" + ".txt";

    // Use our download method
    Download(myList, filename);
}

// We can define a static method to be able to reuse this later for other DACs
public static void Download(List<string> lines, string name)
{
    var bytes = default(byte[]);

    // Write all lines to stream
    using (MemoryStream stream = new MemoryStream())
    {
        StreamWriter sw = new StreamWriter(stream);
        foreach (string line in lines)
        {
            sw.WriteLine(line);
        }
        sw.Close();

        stream.Position = 0;
        bytes = stream.ToArray();
    };

    // Save content to file object
    PX.SM.FileInfo textDoc = new PX.SM.FileInfo(name, null, bytes);

    if (textDoc != null)
    {
        // Trigger file download
        throw new PXRedirectToFileException(textDoc, true);
    } else {
      //TODO: You could raise an exception here also to notify the user
      PXTrace.WriteInformation("Could not generate file");
    }
}
1
votes

I utilize UploadFileMaintenance to do this. I'm not sure if this will meet your needs, but here is the core of my code that works for me.

byte[] labelBytes = Encoding.ASCII.GetBytes(myLabelData);
if(labelBytes.Length > 0)
{
    string filename = "label-" + Guid.NewGuid().ToString() + ".txt";
    PX.SM.FileInfo labelFileInfo = new FileInfo(filename, null, labelBytes);

    UploadFileMaintenance upload = PXGraph.CreateInstance<UploadFileMaintenance>();

    if (upload.SaveFile(labelFileInfo))
    {
        string targetUrl = PXRedirectToFileException.BuildUrl(labelFileInfo.UID);
        throw new PXRedirectToUrlException(targetUrl, "Print Labels");
    }
}
0
votes

Hi Markoan your code helped me to create the textfile but the content of the textfile is getting repeated with the first record of the data-view. My reord type have only three values "00" for the first record "01" for the 2nd to n-1 record and "99" for the nth record.

Though I have made few changes to your code

// We need at least these, listing them for reference
using PX.Data;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;

// This is your dataview
public PXSelect<MayBankGIRO> Document; 

// Your action delegate
public PXAction<MayBankGiroFilter> createTextFile;

[PXUIField(DisplayName = "Create Text File")]
[PXButton()]
public virtual IEnumerable CreateTextFile(PXAdapter adapter)
{
    // You can use this method to print debug information for your customizations
    // Just remove when you are done testing
    PXTrace.WriteInformation("Generating records");

    // We will build the content as a string list first
    List<string> myList = new List<string> { };

    // If the value of 'ReordType' can change for each record, you don't need this
    MayBankGIRO giroObject = this.Document.Current;

   foreach (MayBankGIRO dacRecord in this.Document.Select())
            {
                // Does 'ReordType' change for each record?
                // if it does you may need to use 'dacRecord.ReordType' in this if instead
                myList.Add(dacRecord.ReordType + "|" + dacRecord.CustomerReferenceNumber + "|" + dacRecord.ClientBatchID + "|" + dacRecord.Country + "|");

            }

    PXTrace.WriteInformation("Generating file");

    // Set the name
    string filename = "DAWN" + ".txt";

    // Use our download method
    Download(myList, filename);
}

// We can define a static method to be able to reuse this later for other DACs
public static void Download(List<string> lines, string name)
{
    var bytes = default(byte[]);

    // Write all lines to stream
    using (MemoryStream stream = new MemoryStream())
    {
        StreamWriter sw = new StreamWriter(stream);
        foreach (string line in lines)
        {
            sw.WriteLine(line);
        }
       // sw.Close(); this was showing some error 

        stream.Position = 0; // "Cannot reach a closed stream" hence i added it in the next line
        bytes = stream.ToArray();
        sw.Close();

    };

    // Save content to file object
    PX.SM.FileInfo textDoc = new PX.SM.FileInfo(name, null, bytes);

    if (textDoc != null)
    {
        // Trigger file download
        throw new PXRedirectToFileException(textDoc, true);
    } else {
      //TODO: You could raise an exception here also to notify the user
      PXTrace.WriteInformation("Could not generate file");
    }
}