Sunday, April 15, 2018

X++ Code to Upload CSV file In Dynamics 365 (Ax 7)

CSV File Upload




class Fcc_ItemMasterUpdateJob
{       
 
    /// <summary>
    /// Runs the class with the specified arguments.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {   
   
        AsciiStreamIo                                   file;
        Array                                           fileLines;
        FileUploadTemporaryStorageResult                fileUpload;
        InventTableModule                               inventTableModule;
        str                                             brand,manufacture,SupplierPart;
        real                                            price,profitpercent,overhead,salescost,previousprice;
        InventTable                                     inventTable,inventTableloc ;
        EcoResProduct                                   ecoResProduct;
        boolean                                          first = true;
        container record;

        fileUpload = File::GetFileFromUser() as FileUploadTemporaryStorageResult;
        file = AsciiStreamIo::constructForRead(fileUpload.openResult());
        if (file)
        {
            if (file.status())
            {
                throw error("@SYS52680");
            }
            file.inFieldDelimiter(',');
            file.inRecordDelimiter('\r\n');
        }
     
        while (!file.status())
        {
            record = file.read();
            if (conLen(record))
            {

                if(first)
                {
                    first = false;
                }
                else
                {
                    inventTable = InventTable::find(conPeek(record,1),true);

                    select forUpdate inventTableModule where inventTableModule.ItemId == inventTable.ItemId
                            && inventTableModule.ModuleType == ModuleInventPurchSales::Sales;

                    select forupdate ecoResProduct where ecoResProduct.recid == inventTable.Product;

                    brand          =  conPeek(record,2);
                    manufacture    =  conPeek(record,3);
                    SupplierPart   =  conPeek(record,4);
                    profitpercent  =  conPeek(record,5);
                    price          =  conPeek(record,6);
                    overhead       =  conPeek(record,7);
                    salescost      =  conPeek(record,8);
                    previousprice  =  conPeek(record,9);

                    inventTable.BrandID  =  brand;
                    inventTable.ManufactureID = manufacture;
                    ecoResProduct.SupplierPartNo = SupplierPart;
                    inventTableModule.OTCSalesProfitPercentage = profitpercent;
                    inventTableModule.price = price;
                    inventTableModule.OTCSalesOverhead = overhead;
                    inventTableModule.OTCSalesCost  = salescost;
                    inventTableModule.OTCSalesPreviousCost = previousprice;


                    inventTable.update();
                    inventTableModule.update();
                    ecoResProduct.update();
                   // info(strFmt("%1 -----  %2 ",conPeek(record,1),brand));
                }
            }
        }
        info("done");
    }

}