Trade Agreement Data Import in Dynamics 365

Hi,

Trade Agreement Data Import in Dynamics 365

Below is a class for Import Sales and Purchase prices in Trade Agreeement

class TAPriceImport
{
    counter     icount,inserted;
    /// <summary>
    /// Runs the class with the specified arguments.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {
        TAPriceImport taPriceImport = new TAPriceImport();
        AsciiStreamIo file;
        Array  fileLines;

        //Select a File for import
        FileUploadTemporaryStorageResult fileUpload;

        fileUpload =File::GetFileFromUser() as FileUploadTemporaryStorageResult;
        file = AsciiStreamIo::constructForRead (fileUpload.openResult());

        if(file)
        {
            if(file.status())
            {
                throw error("@SYS52680");
            }
            file.inFieldDelimiter(',');
            file.inRecordDelimiter('\r\n');
        }
     
        taPriceImport.ReadFile(file);
     
    }

    // Read a CSV file for import
    public void ReadFile(AsciiStreamIo file)
    {
        container record;
        String10 First ="Yes";
        while(!file.status())
        {
            record=file.read();
            //Skip Header
            if(First=="Yes")
            {
                First="No";
            }
            else if(First=="No")
            {
                if(conLen(record))
                {
                    this.ImportSalesPrice(record);
                    //info(strFmt("Journal Number %1 - AccountCode %2",conPeek(record,1),conPeek(record,2)));
                }
            }

        }
        info(strfmt("%1 records inserted out of %2",inserted,icount));
    }

    public void  ImportSalesPrice(container readCon)
    {
        //To create the purchase trade agreement lines.
        //Forthis, just create the Journal header first and import the lines by using this job
        //  container   readCon;
        itemId      itemId;
        AmountCur   amount;str         strFromdate,strTodate,type;
        TransDate   fromDate, toDate;
        PurchUnit   purchUnitId;
        SalesUnit Unit;
     
        PriceDiscModule module;
        PriceType relation;
        str  straccountCode;
        str strmodule;
        str strrelation;
        PriceDiscJournalNum journalNum;
        TableGroupAll TableGroupAll;
        PriceDiscAdmTrans   priceDiscAdmTrans;
        InventDim           inventDim;
        InventDimId         inventDimId;
        CurrencyCode        currencyCode;
   
        PriceDiscAccountRelation accountRelation;
        PriceDiscItemCode itemCode;
        PriceDiscItemRelation itemRelation;
        InventSiteId  inventSiteId;
        InventLocationid inventLocationid;
     
                 
        if(readCon)
        {
            strrelation = conPeek(readCon,1);
            strmodule = conPeek(readCon,2);
            journalNum = conPeek(readCon,3);
            straccountCode = conPeek(readCon,4);
            accountRelation= conPeek(readCon,5);
            itemCode= conPeek(readCon,6);
            itemRelation= conPeek(readCon,7);
            inventSiteId= conPeek(readCon,8);
            inventLocationid= conPeek(readCon,9);
            currencyCode = conPeek(readCon, 10);
            Unit = conPeek(readCon, 11);
            strFromdate = conPeek(readCon, 12);
            strTodate = conPeek(readCon, 13);
       
                     
            fromDate = dateNull();
            toDate = dateNull();
            if (strFromdate)
                            fromDate = str2Date(strFromdate,123);
            if (strTodate)
                            toDate = str2Date(strTodate,123);


            //Validation         

            // Currency
            if (currencyCode && !Currency::exist(currencyCode))
            {
                checkFailed(strFmt("Currency '%1' is not valid", currencyCode));
                return ;
            }

            // Site id
            if (inventSiteId && !InventSite::exist(inventSiteId))
            {
                checkFailed(strFmt("Site '%1' is not valid", inventSiteId));
            }

            // Inventlocation id
            if (inventLocationid && !InventLocation::exist(inventLocationid))
            {
                checkFailed(strFmt("Warehouse '%1' is not valid", inventLocationid));
            }
             
            // Unit Id
            if (salesUnit && !UnitOfMeasure::findBySymbol(salesUnit))
            {
                checkFailed(strFmt("Unit '%1' is not valid", salesUnit));
            }

            // -- Validate from date and to date
            if(str2Date(strFromdate, 123) == dateNull())
            {
                checkFailed("@SYS39559");
                return;
            }
                else if(str2Date(strTodate, 123) != dateNull() && str2Date(strFromdate, 123) > str2Date(strTodate, 123))
            {
                checkFailed("@SYS16982");
                return;
            }
                //<-- -- Validate from date and to date

            ttsbegin;

         
                priceDiscAdmTrans.clear();
                priceDiscAdmTrans.JournalNum        = journalNum;
                priceDiscAdmTrans.AccountCode       =  str2Enum(priceDiscAdmTrans.AccountCode ,straccountCode);
             
                priceDiscAdmTrans.AccountRelation =accountRelation;
                priceDiscAdmTrans.relation          = relation ;
                //  priceDiscAdmTrans.Amount            = tradeAgreement.Calculated;
            priceDiscAdmTrans.Currency          = currencyCode;
            priceDiscAdmTrans.FromDate          = fromDate;
            priceDiscAdmTrans.Module            = str2Enum(priceDiscAdmTrans.Module,strModule);
            priceDiscAdmTrans.ToDate =toDate;
            priceDiscAdmTrans.ItemCode      = itemCode; //TableGroupAll::Table;
            priceDiscAdmTrans.ItemRelation = itemRelation;
            priceDiscAdmTrans.UnitId =salesUnit;

            inventDim.clear();
            inventDim.InventSiteId = inventSiteId;
            inventDim.InventLocationId =inventLocationid;
            inventDimId = inventDim::findOrCreate(inventDim).inventDimId;

            priceDiscAdmTrans.InventDimId =inventDimId;
                   
         
            priceDiscAdmTrans.insert();

            ttsCommit;
            icount++;

            inserted++;

        }

             
        }   

}

*******************
Sample File


Relation Module Journal Number AccountCode
AccountRelation ItemCode ItemRelation Site Warehouse  Currency Unit FromDate ToDate
Price (sales) Customer  000031 All
Table A0001 1      11 Pcs 10.01.2017
Price (sales) Customer  000031 All
Table A0002 1      11 Pcs 10.01.2017
Price (sales) Customer  000031 All
Table M00001     2 11 Pcs 10.01.2017




AX 2012
Reference
Reference
Reference

Comments