blog for Dynamics Axapta

Hi Everyone,

Here I posted the X++ code for Inserting Text File values into Invent Table.

Inserting Values to Invent Table from Text File

    FilenameOpen               filename;
    dialogField                     dialogFilename;
    Dialog                            dialog;
    TextIO                           file;
    container                       con;
    InventTable                   inventTable1,inventTable2;
    InventTableModule       inventTableModule;
    InventItemGroup           inventItemGroup,inventItemGroup1;
    ItemGroupId                  itemGroupId,itemGroupId1;
    ItemId                            itemId;
    str                                  conItem,conItemGroup;
    #File
    ;
 
 
    dialog                     =   new Dialog("ISIS Bulk Upoad");
    dialogFilename      =   dialog.addField(typeId(FilenameOpen));
 
    dialog.filenameLookupFilter(["@SYS15896",#Mactxt]);  //Create a macro in #File to open all text files(*.txt)
    dialog.filenameLookupTitle("Upload from Text File");
    dialog.caption("Upload from text file");
 
    dialogFilename.value(filename);
 
    if(!dialog.run())
        return;
 
    filename            =   dialogFilename.value();
 
    try
    {
   
    file                = new TextIO(filename, #IO_READ);
 
    if (!file)
    throw Exception::Error;
 
    file.inRecordDelimiter(‘\n’);  //For Next Record
    file.inFieldDelimiter(‘\t’);  //For Next Column value 
 
    ttsbegin;
 
        while(file.status() == IO_STATUS::OK)
        {
            con                   = file.read();
 
            conItem           = conpeek(con,1);
            conItemGroup = conpeek(con,3);
 
            if(conItem!="0")
            {
 
            itemId                   = substr(conItem,1,8);
            ItemGroupId         = substr(conItemGroup,2,13);
 
            //Item Group Table Insert
 
            inventItemGroup1    = inventItemGroup::find(ItemGroupId);
 
                if(inventItemGroup1)
                {
                    info(strfmt("ItemGroup (%1) already exist",ItemGroupId));
                }
                else
                {
                    inventItemGroup.ItemGroupId = ItemGroupId;
                    inventItemGroup.insert();
                    info(strfmt("ItemGroup (%1) Inserted Successfully",ItemGroupId));
                }
                //Item Group Table Insert End
 
                //Invent Table Insert
 
                inventTable1     = inventTable::find(ItemId);
                itemGroupId1    = inventTable::find(ItemId).ItemGroupId;
 
 
                if(inventTable1)
                {
                   if(itemGroupId1 != ItemGroupId)
                   {
                        inventTable2.ItemGroupId = ItemGroupId;
                        inventTable2.update();
                        info(strfmt("ItemGroup for Item (%1) Updated successfully",itemId));
                   }
                   else
                   {
                        info(strfmt("Item (%1) and ItemGroup (%2) already exist",itemId,itemGroupId));
                   }
                }
                else
                {
                    //Invent Table Insert
                    inventTable2.initValue();
                    inventTable2.ItemId              = itemId;
                    inventTable2.ItemGroupId    = ItemGroupId;
                    inventTable2.DimGroupId     = "N – W";
                    inventTable2.ModelGroupId = Enum2str(InventModel::FIFO);
                    inventTable2.insert();
 
                    select forupdate inventTableModule;
                    // Cost
                    inventTableModule.initValue();
                    inventTableModule.ItemId              = ItemId;
                    inventTableModule.ModuleType    = ModuleInventPurchSales::Invent;
                    inventTableModule.insert();
                    // Purchase order
                    inventTableModule.initValue();
                    inventTableModule.ItemId              = ItemId;
                    inventTableModule.ModuleType    = ModuleInventPurchSales::Purch;
                    inventTableModule.insert();
                    // Sales order
                    inventTableModule.initValue();
                    inventTableModule.ItemId              = ItemId;
                    inventTableModule.ModuleType    = ModuleInventPurchSales::Sales;
                    inventTableModule.insert();
 
                    //Invent Item Location Insert
                    inventItemLocation.ItemId           = ItemId;
                    inventItemLocation.inventDimId  = InventDim::inventDimIdBlank();
                    inventItemLocation.insert();
 
                    info(strfmt("Item and ItemGroup for Item (%1) Inserted successfully",itemId));
                }
            }//Null check
        }//While
    ttscommit;
    }
    catch(Exception::Error)
    {
        Error("Upload Failed");
    }

Text File Format that I used for Upload :

Leave a comment