D365FO – AX – Import CSV file in a runnable class in X++

This is a simple example to import a csv file in arunnable class in C365FFO

class UpdateProjectQuotationStatusFromConfirmedToSent
{
    public static void main(Args _args)
    {
        //Declarations
        Dialog                              dialog;
        AsciiStreamIo                       file;
        Array                               fileLines;
        FileUploadTemporaryStorageResult    fileUpload;
        QuotationId                         quotationId;
        DialogField                         quotationStatus;
        DialogField                         fileSt;
        container record;
        SalesQuotationTable         salesQuotationTableLocal;
        SalesQuotationStatus        SalesQuotationStatusNew;
        fileUpload = File::GetFileFromUser() as FileUploadTemporaryStorageResult;
        file = AsciiStreamIo::constructForRead(fileUpload.openResult());
        if (file)
        {
            if (file.status())
            {
                throw error("@SYS52680");
            }
            file.inFieldDelimiter(';');
            //file.inRecordDelimiter('\r');
        }
        
        while (!file.status())
        {
            record = file.read();
            if (conLen(record))
            {
                SalesQuotationStatus salesQuotationStatus;
                quotationId = conPeek(record,1);
                SalesQuotationStatusNew = str2Enum(SalesQuotationStatusNew,conPeek(record,2));
                SalesQuotationTable salesQuotationTable = SalesQuotationTable::find(quotationId, true);
                if(!salesQuotationTable)
                {
                    info("Quotation Id : " + salesQuotationTable.QuotationId + " does not exists");
                }
                else
                {
                    salesQuotationTable.quotationStatus = SalesQuotationStatusNew;
                    try 
                    {
                        if(salesQuotationTable.quotationStatus == SalesQuotationStatus::Confirmed)
                        {
                            ttsbegin;
                            salesQuotationTable.doupdate();
                            ttscommit;
                            info("Quotation Id : " + salesQuotationTableLocal.quotationId + " updated");
                        }   
                        else
                        {
                            info("Skipped Quotation Id : " + salesQuotationTableLocal.quotationId + " because original status is not Confirmed");
                        }
                    }
                    catch
                    {
                        error("ERROR - Quotation Id : " + salesQuotationTableLocal.quotationId + " not updated");
                    }
                }
            }
        }
        info("done");
    }
}

You can run the class in various ways. If you know how read my article : https://d365ffo.com/2021/07/15/ax-d365fo-running-a-runnable-class-through-url-in-dynamics-365-for-operations/

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo di WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Google photo

Stai commentando usando il tuo account Google. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...