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/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s