Friday 12 April 2013

AX 2009 : Read CSV file in x++


static void ReadCSVFile(Args _args)
{
    Dialog                  dialog;
    DialogField             dialogFileName;
    SysOperationProgress    simpleProgress;
    Filename                filename;
    CommaIo                 csvFile;
    container               readCon;
    Container               filterCriteria;
    int                     numLines;
    int                     cnt;
    FileIOPermission        permission;
    TextIO                  textIO;
    Str                     _taskStat;

    SugarRecordId       _sugarRecId; //field variable
    PaymSchedule        _paymentSchedule; // Table instance
    PaymScheduleHistory _paymScheduleHistory; //Table instance
    textBuffer tb = new textBuffer();
    #File
    #avifiles
    ;
    //salesTable = _args.record();

    dialog = new Dialog("Importing Text File");
    dialogFileName = dialog.addField(typeid(Filenameopen), "File Name");
    filterCriteria = ['*.csv'];
    filterCriteria = dialog.filenameLookupFilter(filterCriteria);
    dialog.run();

    if (dialog.run())
    filename = dialogFileName.value();

    if(!filename)
    {
        info("Filename must be filled");
        throw(" ");
    }

    permission = new fileIOpermission(filename,#io_read);
    permission.assert();
    textIO = new TextIO(filename,#io_read);
    if (!textIO)
    {
        throw error("Error reading file");//@ABC4");
    }
    tb.fromFile(filename);//File name  with Path ...
    numLines = tb.numLines();
    csvFile = new CommaIo(filename, 'r');
    csvFile.inFieldDelimiter("  "); // Delimiter...
    simpleProgress = SysOperationProgress::newGeneral(#aviUpdate, 'Importing data'/*@ABC5'*/,1000);

    if (csvFile)//Checking for csv file.
    {
        readCon = csvFile.read();//reading the file.
        ttsbegin;
        for(cnt=1;cnt<=numLines;cnt++)
        {
            readCon     = str2Con(tb.nextToken(true));
            if(cnt>1)
            {
               _sugarRecId = conpeek(readCon,9);
               _paymentSchedule = PwC_PaymSchedule::findBySugarRecId(_sugarRecId);
               _paymScheduleHistory = AUR_PaymScheduleHistory::findBySugarRecId(_sugarRecId);
               if(_paymentSchedule)
               {
                    delete_from _paymentSchedule where _paymentSchedule.SugarRecordId == _sugarRecId;

               }
               if(_paymScheduleHistory)
               {
                    select forupdate _paymScheduleHistory where _paymScheduleHistory.SugarRecordId == _sugarRecId;
                     _paymScheduleHistory.SugarRecordId = _sugarRecId;
                    _paymScheduleHistory.SalesId = conpeek(readCon,1);
                    _paymScheduleHistory.PaymMode = conpeek(readCon,2);
                    _paymScheduleHistory.PaymReference = conpeek(readCon,3);
                    _paymScheduleHistory.InstrumentDate = str2date(conpeek(readCon,4),213);
                    _paymScheduleHistory.InstrumentCurrencyCode = conpeek(readCon,5);
                    _paymScheduleHistory.InstrumentAmount = conpeek(readCon,6);
                    _paymScheduleHistory.BankDetails = conpeek(readCon,7);
                    //_paymScheduleHistory.dataAreaId = conpeek(readCon,8);

                    _paymScheduleHistory.Accepted = conpeek(readCon,10);
                    _paymScheduleHistory.ClearedAmount = conpeek(readCon,11);
                    _paymScheduleHistory.ClearanceStatus = conpeek(readCon,12);
                    _paymScheduleHistory.PaymType = conpeek(readCon,14);
                    _paymScheduleHistory.PaymId = conpeek(readCon,15);
                    _paymScheduleHistory.Remarks = conpeek(readCon,16);
                    _paymScheduleHistory.AUR_Reason = conpeek(readCon,17);

                    _paymScheduleHistory.update();

                    _taskStat = strfmt("History updated");
               }
               else if(_paymentSchedule && !_paymScheduleHistory)
               {
                    //_paymScheduleHistory.SugarRecordId = _sugarRecId;

                    _paymScheduleHistory.SalesId = conpeek(readCon,1);
                    _paymScheduleHistory.PaymMode = conpeek(readCon,2);
                    _paymScheduleHistory.PaymReference = conpeek(readCon,3);
                    _paymScheduleHistory.InstrumentDate = str2date(conpeek(readCon,4),213);
                    _paymScheduleHistory.InstrumentCurrencyCode = conpeek(readCon,5);
                    _paymScheduleHistory.InstrumentAmount = conpeek(readCon,6);
                    _paymScheduleHistory.BankDetails = conpeek(readCon,7);
                    //_paymScheduleHistory.dataAreaId = conpeek(readCon,8);
                    _paymScheduleHistory.SugarRecordId = _sugarRecId;

                    _paymScheduleHistory.Accepted = conpeek(readCon,10);
                    _paymScheduleHistory.ClearedAmount = conpeek(readCon,11);
                    _paymScheduleHistory.ClearanceStatus = conpeek(readCon,12);
                    _paymScheduleHistory.PaymType = conpeek(readCon,14);
                    _paymScheduleHistory.PaymId = conpeek(readCon,15);
                    _paymScheduleHistory.Remarks = conpeek(readCon,16);
                    _paymScheduleHistory.AUR_Reason = conpeek(readCon,17);

                    _paymScheduleHistory.insert();

                    _taskStat = strfmt("History inserted");
               }
               //info(_paymentSchedule.SugarRecordId);
            }
        }
        info(_taskStat);
        ttscommit;

    }

}

No comments:

Post a Comment