adsenseheader

Tuesday, October 15, 2019

File upload and export data to excel

Hi,

Below code would be helpful to upload a file in Dynamics 365 for operations and take necessity actions on the data from the file.

Export the result to an excel file

class ReadAndExportData
{     
    NoYesId printOut;
    NoYesId deleteInventTransId;
    FileUploadTemporaryStorageResult fileVal;

    public NoYesId parmPrintOut(NoYesId _printOut = printOut)
    {
        printOut = _printOut;

        return printOut;
    }

    public NoYesId parmDeleteTransId(NoYesId _delete = deleteInventTransId)
    {
        deleteInventTransId = _delete;

        return deleteInventTransId;
    }

    public FileUploadTemporaryStorageResult parmgetFile(FileUploadTemporaryStorageResult _file = fileVal)
    {
        fileVal = _file;

        return fileVal;
    }

    public void run()
    {
        #define.filename("LotID.csv")
        AsciiStreamIo                                   file;
        Array                                           fileLines;
        FileUploadTemporaryStorageResult                fileUpload;
        PurchTable purchTable;
        PurchLine purchLine;
        InventTransOrigin inventTransOrigin;
        InventTrans inventTrans;
        boolean printDoc = false;

        CommaStreamIo io = CommaStreamIo::constructForWrite();
        const str filename = #filename;
        fileUpload = fileVal;

        if (this.parmPrintOut() && !this.parmDeleteTransId())
        {
            io.writeExp(["@AccountsPayable:PurchaseOrderId", "@SYS75853"]);

while select PurchId from purchTable
                where purchTable.PurchStatus == PurchStatus::Received
             
                where purchLine.PurchId == purchTable.PurchId
            {
                io.writeExp([purchLine.PurchId, purchLine.ItemId]);
            }

            // get stream
            System.IO.Stream stream = io.getStream();
            stream.Position = 0;

            // read stream
            System.IO.StreamReader reader = new  System.IO.StreamReader(Stream);
            str csvFileContent = reader.ReadToEnd();

            //send file to user for save
            File::SendStringAsFileToUser(csvFileContent,filename);
        }

        if (fileUpload != null)
        {
            file = AsciiStreamIo::constructForRead(fileUpload.openResult());
            if (file)
            {
                if (file.status())
                {
                    throw error("@SYS52680");
                }
                file.inFieldDelimiter(',');
                file.inRecordDelimiter('\r\n');
            }

            container record;
            io.writeExp(["@AccountsPayable:PurchaseOrderId", "@SYS75853"]);

            if (this.parmPrintOut() || this.parmDeleteTransId())
            {
                while (!file.status())
                {
                    record = file.read();
                    if (conLen(record))
                    {
                        inventTransOrigin = InventTransOrigin::findByInventTransId(conPeek(record,1));

                        if (inventTransOrigin.RecId)
                        {
                            if (this.parmPrintOut() && this.parmDeleteTransId())
                            {
                                printDoc = true;
                                io.writeExp([inventTransOrigin.InventTransId, inventTransOrigin.ItemId]);
                            }

                        }
                    }
                }
                if (printDoc)
                {
                    // get stream
                    System.IO.Stream stream = io.getStream();
                    stream.Position = 0;

                    // read stream
                    System.IO.StreamReader reader = new  System.IO.StreamReader(Stream);
                    str csvFileContent = reader.ReadToEnd();

                    //send file to user for save
                    File::SendStringAsFileToUser(csvFileContent,filename);
                }
            }
        }
    }

    public static void main(Args _args)
    {
        const str controlName = 'ItemFile';
        Dialog dialog = new Dialog("PurchaseOrderCorrection");
        DialogGroup dialogGroup;
        DialogField dialogPrintOut;
        DialogField dialogDelete;
        FormBuildControl            formBuildControl;
        ReadAndExportData postingIssue = new ReadAndExportData();

        dialogGroup = dialog.addGroup("PurchaseOrderCorrection");
        dialogGroup.columns(2);

        dialogPrintOut = dialog.addField(extendedTypeStr(NoYesId));
        dialogPrintOut.label("@SYS12608");

        dialogDelete   = dialog.addField(extendedTypeStr(NoYesId));
        dialogDelete.label("DeleteCorruptedPO");

        formBuildControl = dialog.formBuildDesign().control(dialogGroup.name());

        FileUploadBuild  dialogFileUpload = formBuildControl.addControlEx(classstr(FileUpload), controlName);
        dialogFileUpload.baseFileUploadStrategyClassName(classstr(FileUploadTemporaryStorageStrategy));
        dialogFileUpload.fileNameLabel("@SYS308842");

        if (dialog.run())
        {
            FileUpload fileUploadControl = dialog.formRun().control(dialog.formRun().controlId(controlName));
            FileUploadTemporaryStorageResult fileUploadResult = fileUploadControl.getFileUploadResult() as FileUploadTemporaryStorageResult;

            postingIssue.parmPrintOut(dialogPrintOut.value());
            postingIssue.parmDeleteTransId(dialogDelete.value());
            postingIssue.parmgetFile(fileUploadResult);
            postingIssue.run();
        }
    }

}

No comments:

Post a Comment