Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested answer

The selected item cannot be used in this context if it is associated with an item model group where Always expense is selected.

(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              locinventJournalTrans;
                        InventJournalTable              inventjournaltable;
                        InventTable                     inventTable;
                        int                             row=1;
                        InventUpd_Estimated             estimated;
                        SalesLine                       SalesLine;
                        //int                             coun;


     str COMVariant2Str(COMVariant _cv, int _decimals = 0, int _characters = 0, int _separator1 = 0, int _separator2 = 0)
    {
        switch (_cv.variantType())
             {
        case (COMVariantType::VT_BSTR):
        return _cv.bStr();

        case (COMVariantType::VT_R4):
        return num2str(_cv.float(),_characters,_decimals,_separator1,_separator2);

        case (COMVariantType::VT_R8):
        return num2str(_cv.double(),_characters,_decimals,_separator1,_separator2);

        case (COMVariantType::VT_DECIMAL):
        return num2str(_cv.decimal(),_characters,_decimals,_separator1,_separator2);

        case (COMVariantType::VT_DATE):
        return date2str(_cv.date(),123,2,1,2,1,4);

        case (COMVariantType::VT_EMPTY):
        return "";

       default:
       throw error(strfmt("@SYS26908", _cv.variantType()));
                        }
               return "";
    }



      //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;
         type = cells.item(row, 1).value().variantType();
         while (cells.item(row 1, 1).value().variantType() != COMVariantType::VT_EMPTY)
        {
    locinventJournalTrans.clear();
    locinventJournalTrans.TransDate       =str2DateDMY(cells.item(row, 1).value().bStr());
    locinventJournalTrans.ItemId          =COMVariant2Str(cells.item(row, 2).value());
    locinventDim.configId                 =COMVariant2Str(cells.item(row, 3).value());
    locinventJournalTrans.initFromInventTable(InventTable::find(locinventJournalTrans.ItemId));
    locinventDim.InventSizeId             =COMVariant2Str(cells.item(row, 4).value());
    locinventDim.InventColorId            =COMVariant2Str(cells.item(row, 5).value());
    locinventDim.InventLocationId         =COMVariant2Str(cells.item(row, 6).value());
    locinventDim.InventSiteId             =COMVariant2Str(cells.item(row, 7).value())  ;
    locinventDim.inventBatchId            =COMVariant2Str(cells.item(row, 8).value());
    locinventDim.wMSLocationId            =COMVariant2Str(cells.item(row, 9).value());
    locinventDim.inventSerialId           =COMVariant2Str(cells.item(row, 10).value());
    locinventJournalTrans.MZNPurchUnit    =COMVariant2Str(cells.item(row, 11).value());
    locinventJournalTrans.Qty             =any2real(cells.item(row, 12).value().bStr());

   locinventJournalTrans.InventDimId      = InventDim::findOrCreate(locInventDim).inventDimId;


        // Insert the data into the appropriate tables.

   if (inventTable.ItemType == ItemType::Item)
   {
    inventTable.insert(true);
    }
    else
   {
    inventTable.ItemType = ItemType::Service;
    checkFailed(strFmt("@FPK270056"));
    }   
//If(InventTable::find(SalesLine.ItemID).isStocked())
//{
//estimated= InventUpd_Estimated::newInventMovement(estimated.updateNow());
//}
  //if (inventTable.ItemType == ItemType::Item && inventTable.isStocked())
//{
    //estimated = InventUpd_Estimated::newInventMovement( estimated.updateNow());
//}

            locinventJournalTrans.insert();

            ttscommit;
           // coun  ;
           // print('coun');
            row  ;
    }

         //info(int2str(coun));

           application.quit();



                            super();
                                InventJournalTrans_ds.refresh();
                                InventJournalTrans_ds.research();

    info('Done');

}

}
               While Import Excel File in Movement Journal there issues create on ItemType::Item and ItemType::Service.. 

  • Fawad Ahmad Profile Picture
    61 on at
    RE: The selected item cannot be used in this context if it is associated with an item model group where Always expense is selected.

    I don't  know how to write this code for as my use.  

  • Suggested answer
    Komi Siabi Profile Picture
    12,772 Most Valuable Professional on at
    RE: The selected item cannot be used in this context if it is associated with an item model group where Always expense is selected.

    Hello Fawad Ahmad,

    You are getting this error for non-inventory items that you are trying to import. You should update your code.

    amazingax.wordpress.com/.../

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... 292,884 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,760 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans