Import CSV file in D365 for Finance and Operation using X++ | CloudFronts

Import CSV file in D365 for Finance and Operation using X++

Below is a simple example for importing data from CSV file in D365 FO using X++:

/// <summary>

/// import color code

/// </summary>

class CFSImportColorCode

{

    /// <summary>

    /// main

    /// </summary>

    /// <param name = “_args”>_args</param>

    public static void main(Args _args)

    {

        AsciiStreamIo                       file;

        Array                               fileLines;

        FileUploadTemporaryStorageResult    fileUpload;

        CFSEcoResColorCode                  colorCode;

        CFSImportColorCode                  importColorCode = new CFSImportColorCode();

        Counter                             counter = 0;

        EcoResColorName                     color;

        #OCCRetryCount

        try

        {

            //Upload a file

            fileUpload  = File::GetFileFromUser() as FileUploadTemporaryStorageResult;

            file        = AsciiStreamIo::constructForRead(fileUpload.openResult());

            if (file)

            {

                if (file.status())

                {

                    throw error(“@SYS52680”);

                }

                file.inFieldDelimiter(‘;’); //separator

                file.inRecordDelimiter(‘\r\n’);

            }

            //Read a CSV File

            container rec;

            ttsbegin;

            while (!file.status())

            {

                counter++;

                rec = file.read();

                if (conLen(rec))

                {

                    color = conPeek(rec, 2);

                    colorCode = CFSEcoResColorCode::find(color);

                    if(!colorCode.RecId)

                    {

                        colorCode.clear();

                        colorCode.Name  = color;

                        colorCode.Code  = conPeek(rec, 1);

                        colorCode.insert();

                    }

                }

            }

            ttscommit;

            info(“Operation complete.”);

        }

        catch (Exception::Deadlock)

        {

            retry;

        }

        catch (Exception::UpdateConflict)

        {

            if (appl.ttsLevel() == 0)

            {

                if (xSession::currentRetryCount() >= #RetryNum)

                {

                    throw Exception::UpdateConflictNotRecovered;

                }

                else

                {

                    retry;

                }

            }

            else

            {

                throw Exception::UpdateConflict;

            }

        }

    }

}


Share Story :

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close