web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Import from CSV (Comma Delimited) file with X++

(1) ShareShare
ReportReport
Posted on by 1,165

To Import data from CSV file "Comma Delimited" for example inventory journal transactions use the below code assuming the csv file include data as the following table:

Item ID Site Warehouse Qty. Unit Price
10000001 Main RM 15 120

static void ImportfromCSV(Args _args)

{

    Dialog      dialog  = new Dialog();

    DialogField dialogField;

    AsciiIo importFile;

    str filePath,fileNameOnly;

    filetype type;

    container record;

    str         Delimiter = ",";

    int totalRecords;

   

    InventDim               InventDim;

    InventJournalTable      journalTable;

    InventJournalTrans      journalTrans;

    InventJournalTableData  journalTableData;

    InventJournalTransData  journalTransData;

   

    dialogField=dialog.addField(extendedTypeStr(FilenameOpen),"Select File","Select file to import");

    dialog.caption("File Picker");

    dialog.filenameLookupFilter(['csv','*.csv']);

    if(!dialog.run())

        return;

    [filePath, fileNameOnly, type] = fileNameSplit(dialogField.value());

    importFile = new AsciiIo(dialogField.value(), 'R');

    if((!importFile) || (importFile.status() != IO_Status::Ok))

    {

        warning("Error in opening import file");

        throw(Exception::Error);

    }

    importFile.inFieldDelimiter(Delimiter);

    if((!importFile) || (importFile.status() != IO_Status::Ok))

    {

        warning("Error in opening log file");

        throw(Exception::Error);

    }

    try

    {

        ttsbegin;

        journalTableData = JournalTableData::newTable(journalTable);

        journalTransData = journalTableData.journalStatic().newJournalTransData(journalTrans,journalTableData);

        journalTable.clear();

        journalTable.JournalId = journalTableData.nextJournalId();

        journalTable.JournalType = InventJournalType::Movement;

        journalTable.JournalNameId = journalTableData.journalStatic().standardJournalNameId(journalTable.JournalType);

        journalTableData.initFromJournalName(journalTableData.journalStatic().findJournalName(journalTable.JournalNameId));

        journalTable.insert();

       

        record = importFile.read();

        while(importFile.status() ==  IO_Status::Ok)

        {

            record = importFile.read();

            if(!record)

                break;

            totalRecords = totalRecords + 1;

            journalTrans.clear();

            journalTransData.initFromJournalTable();

 

            journalTrans.TransDate          = today();

            journalTrans.ItemId             = conPeek(record,1);

            journalTrans.Qty                = conPeek(record,4);

            journalTrans.CostPrice          = conPeek(record,5);

            journalTrans.CostAmount         = JournalTrans.Qty * JournalTrans.CostPrice;

           

            // Dimension details

            inventDim.clear();

            inventDim.InventLocationId  = conPeek(record,2);

            inventDim.InventSiteId      = conPeek(record,3);

            journalTrans.InventDimId = InventDim::findOrCreate(inventDim).inventDimId;

           

            journalTransData.create();

        }

        ttscommit;

    }

    catch(Exception::Error)

    {

        Throw(Exception::Error);

    }

    info(strFmt("Total Read Records = %1",totalRecords));

}


*This post is locked for comments

I have the same question (0)
  • Ibrahim Salah Mohamed Profile Picture
    1,165 on at

    worked?

  • axerp Profile Picture
    2,600 on at

    What about the Financial dimensions also import along with the Inventory journals ?

    Thanks in advance.

  • orloffb Profile Picture
    5 on at

    Hi Ibrahim, thanks for the useful cool. I see that you are able to begin importing starting with the second line (skipping the title fields). I was just wondering how I would go about starting to read from a different line, say line 4?

    Thanks,

    Ben

  • Community Member Profile Picture
    on at

    Salaam Ibrahim,

    This sound really cool but however I have tried using the codes and ending up with the below error;


     

    Am I missing anything? I copied the exact code.

    My data in my csv file are as shown;

    Item ID;Site;Warehouse;Qty;Unit Price
    MVAC001;AVI-HO;VACC;15;120

    Note that I am using ' ; ' as delimiter.

     str             Delimiter = ";";


    Can you advise please?

    Many thanks,

    Nadeem

     

  • PA-22040759-0 Profile Picture
    6,194 on at

    Ibrahim, thanks for sharing.

    Using Ax... classes during import will help you create data correctly and fill in a lot blanks for you.

    Have a look at the AxInventJournalTable and AxInventJournalTrans classes.

    If you want to be an import connoisseur, you could write a transformation class to transform the CSV to XML which the AIF could consume through a service :-)

    Also look out for client/server communication. If your code runs from the client, you'll get a call between client and server for each line. That could end up giving you a lot of traffic.

  • Community Member Profile Picture
    on at

    Worked well for inventTable updates! Thanks!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans