Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Unanswered

Import Excel data in invent movement Journal in lines.

(0) ShareShare
ReportReport
Posted on by 61

void clicked()
{

                        Dialog                          dialog;
                        DialogField                     dialogField;
                        FileName                        filename;
                        SysExcelApplication             application;
                        SysExcelWorkbooks               workbooks;
                        SysExcelWorkbook                workbook;
                        SysExcelWorksheets              worksheets;
                        SysExcelWorksheet               worksheet;
                        SysExcelCells                   cells;
                        COMVariantType                  type;

                        InventDim                                   locinventDim,locToInventDim;
                        InventJournalTrans                          inventJournalTrans;
                        //InventJournalTable                          inventjournaltable;
                        InventTable                                 inventTable;
                        InventJournalName                         inventJournalName;
                        InventJournalNameid                           inventJournalNameid;
                        DimensionAttributeValueCombination          dimAttrValueCombo;
                        JournalId                                   journalId1;
                          InventTransferLine             inventTransferLine;
                        int                                  row=2;
                        int                                 numOfLines = 0;
                       // str                         _mainAccount='110110';
                       //int                             coun;
                        //JournalCheckPost inventJournalCheckPost;


      //Define File name.
          dialog = new Dialog("Select file");
          dialogField = dialog.addField(extendedTypeStr("FileNameOpen"),"Select File Source");
          if (dialog.run())     {
          fileName = dialogField.value();
              }
          if (fileName)
              {
           application = SysExcelApplication::construct();
           workbooks = application.workbooks();
          //Open Excel workbook.
          try    {
                   workbooks.open(filename);
                  }
          catch (Exception::Error)    {
                 throw error("File cannot be opened.");
                 }
             //Open Excel Sheet
             workbook = workbooks.item(1);
             worksheets = workbook.worksheets();
             worksheet = worksheets.itemFromNum(1);//which shows excel sheet number.
             cells = worksheet.cells();

        ttsbegin;
                  try
{
        //Below code creates journal header
        inventJournalTable.clear();



        inventJournalTable.JournalNameId = this.COMVariant2Str(cells.item(row,1).value());
        inventJournalName = InventJournalName::find(this.COMVariant2Str(cells.item(row,1).value()));
        inventJournalTable.initFromInventJournalName(inventJournalName);
        info(strFmt("JournalNameId: %1", inventJournalTable.JournalNameId));
        inventJournalTable.Description    =this.COMVariant2Str(cells.item(row,2).value());
        inventJournalTable.SystemBlocked = true;
        inventJournalTable.insert();
    }
                  catch             //(Exception::Error)
                  {
                      throw error("error here");
                   //info(strFmt("Error while importing data. Journal name ID: %1", inventjournaltable.journalNameId));
                  }

         ttscommit;

         type = cells.item(row, 1).value().variantType();
         while (cells.item(row, 1).value().variantType() != COMVariantType::VT_EMPTY)


         {
       //**********************************************************Start
         ttsbegin;
         //Below code creates  lines
        inventJournalTrans.clear();
        inventJournalTrans.initFromInventJournalTable(inventJournalTable);
        inventJournalTrans.TransDate            =cells.item(row, 3).value().date();                               //systemDateGet();
        info(strFmt(this.COMVariant2Str(cells.item(row, 4).value())));
        inventJournalTrans.ItemId               =this.COMVariant2Str(cells.item(row, 4).value());                      //Replace the ItemId according to your data
        inventJournalTrans.initFromInventTable(InventTable::find(inventJournalTrans.ItemId));

        inventJournalTrans.Qty                  =any2real(cells.item(row, 16).value().bStr());

             info(strFmt(this.COMVariant2Str(cells.item(row, 18).value())));
              info(strFmt(this.COMVariant2Str(cells.item(row, 19).value())));
        inventJournalTrans.MZNQty               =any2real(cells.item(row, 18).value().bStr());        //Replace the Qty according to your data
        inventJournalTrans.CostPrice            =any2real(cells.item(row, 19).value().bStr());
        inventJournalTrans.InventDimId = InventdIm::findOrCreate(locinventDim).inventDimId;
            locinventDim = InventDim::findOrCreate(locinventDim);





            //***** S
    locinventDim.initFromInventTable(inventJournalTrans.inventMovement().inventTable(),InventItemOrderSetupType::Invent,locinventDim);
    locinventDim.configId                 =this.COMVariant2Str(cells.item(row, 5).value());
    locinventDim.InventSizeId             =this.COMVariant2Str(cells.item(row, 6).value());
    locinventDim.InventColorId            =this.COMVariant2Str(cells.item(row, 7).value());
    locinventDim.InventStyleId            =this.COMVariant2Str(cells.item(row, 8).value());
    locinventDim.inventBatchId            =this.COMVariant2Str(cells.item(row, 11).value());
    locinventDim.wMSLocationId            =this.COMVariant2Str(cells.item(row, 12).value());
    locinventDim.inventSerialId           =this.COMVariant2Str(cells.item(row, 13).value());
    locinventdim.InventSiteId             =this.COMVariant2Str(cells.item(row, 9).value());                  //Replace the InventSiteId (Site) according to your data
    locinventdim.InventLocationId         =this.COMVariant2Str(cells.item(row, 10).value());                //Replace the InventLocationId (Warehouse) according to your data
    inventJournalTrans.InventDimId        =InventdIm::findOrCreate(locinventDim).inventDimId;
    inventJournalTrans.MZNPurchUnit       =this.COMVariant2Str(cells.item(row, 17).value());
             //*****    E

                   dimAttrValueCombo.clear();
        select firstonly RecId from  dimAttrValueCombo
            where dimAttrValueCombo.MainAccount == MainAccount::findByMainAccountId(this.COMVariant2Str(cells.item(row, 21).value())).RecId //Replace the MainAccountId according to your data
            && dimAttrValueCombo.DisplayValue == this.COMVariant2Str(cells.item(row, 21).value());  //Replace the MainAccountId according to your data
            inventJournalTrans.LedgerDimension = dimAttrValueCombo.RecId;

            //inventJournalTrans.insert();
        //Insert line to movement journal if quantity is non-zero
        if(inventJournalTrans.MZNQty > 0)
        {
             inventJournalTrans.MZNQty          = inventJournalTrans.MZNQty * -1;

        }

              numOfLines  ;
              inventJournalTrans.insert();

              row  ;
             //numOfLines  ;

             journalId1=InventJournalTable.JournalId;
             info(strFmt(journalId1));

            update_recordSet inventJournalTable
            setting SystemBlocked = false, NumOfLines = numOfLines
            where inventJournalTable.JournalId == journalId1;

        ttscommit;


       info('Done');



       //********************************************************** END

            //coun  ;
            //print('coun');
         //info(int2str(coun));

        inventJournalTable.NumOfLines = numOfLines;
       // inventJournalTable.update();

          //info(strFmt("Movement Journal ID: %1", inventJournalTable.JournalId));




                  //  application.quit();

                  super();

                            InventJournalTable_ds.refresh();
                            InventJournalTable_ds.research();

}
}
}

But there is a calculation behind MZNqty and Costprice fields. The issue is that records of these fields not import in lines.

I am beginner  at using Dynamic AX 2012.  If few changes are  required in my code. please guide me .

But there is calculation behind MZNqty  and Costprice fields.The issue is that records of these fields not import in lines.

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,140 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,895 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans