Saturday, July 9, 2011

Data-upload into AX from CSV file(comma delimited)

Exportfile for AOT version 1.0 or later
Formatversion: 1
***Element: CLS
; Microsoft Dynamics AX Class: ImportDataCSV unloaded
; --------------------------------------------------------------------------------
  CLSVERSION 1
 
  CLASS #ImportDataCSV
    PROPERTIES
      Name                #ImportDataCSV
      Extends             #RunBase
      RunOn               #Called from
    ENDPROPERTIES
   
    METHODS
      Version: 3
      SOURCE #classDeclaration
        #class ImportDataCSV extends RunBase
        #{
        #    FilenameOpen    filename;
        #
        #    CustAccount     custAccount;
        #    CustGroupId     custGroup;
        #    CurrencyCode    currencyCode;
        #
        #    DialogField     dialogFileName;
        #    #define.CurrentVersion(1)
        #    #localmacro.CurrentList
        #            filename
        #    #endmacro
        #}
      ENDSOURCE
      SOURCE #dialog
        #/// <summary>
        #/// The dialog method defines the dialog. The dialog contains a file name.
        #/// </summary>
        #/// <returns>
        #/// The method dialog returns the defined dialog.
        #/// </returns>
        #public Object dialog(DialogRunbase _dialog, boolean _forceOnClient)
        #{
        #    DialogRunbase dialog;
        #    ;
        #
        #    dialog = super(_dialog, _forceOnClient);
        #    dialog.addGroup("Choose File to Upload");
        #    dialogFilename  = dialog.addFieldValue(typeid(FilenameOpen), filename);
        #
        #    return dialog;
        #}
      ENDSOURCE
      SOURCE #getFromDialog
        #public boolean getFromDialog()
        #{
        #    boolean ret;
        #
        #    ret = super();
        #
        #    filename    = dialogFilename.value();
        #
        #    if (!WinApi::fileExists(filename))
        #        ret = checkFailed(strfmt("@SYS18678", filename));
        #
        #    return ret;
        #}
        #
      ENDSOURCE
      SOURCE #pack
        #public container pack()
        #{
        #    return [#CurrentVersion, #CurrentList];
        #}
        #
      ENDSOURCE
      SOURCE #parmFilename
        #public FilenameOpen parmFilename(FilenameOpen _filename = filename)
        #{
        #    ;
        #    filename = _Filename;
        #
        #    return filename;
        #}
        #
      ENDSOURCE
      SOURCE #run
        #public void run()
        #{
        #    AsciiIO             file;
        #    Container           conData;
        #    boolean             first = false;
        #    CustTable           custTable;
        #    ;
        #
        #    file    = new AsciiIO(filename,"R");
        #    if(!file && file.status() != IO_Status::Ok)
        #    {
        #        throw error(strfmt("@SYS19312",filename));
        #    }
        #
        #    file.outRecordDelimiter('\n');
        #    file.inFieldDelimiter(',');
        #
        #    while (file.status() == IO_status::Ok)
        #    {
        #        conData = file.read();
        #
        #        if(first)
        #        {
        #            if(conData != connull())
        #            {
        #                custAccount     = conpeek(conData,1);
        #                custGroup       = conpeek(conData,2);
        #                currencyCode    = conpeek(conData,3);
        #
        #                if(!CustTable::exist(custAccount))
        #                {
        #                    CustTable.initValue();
        #                    CustTable.AccountNum    = custAccount;
        #                    if(CustGroup::exist(custGroup))
        #                    {
        #                        CustTable.CustGroup = custGroup;
        #                    }
        #                    if(Currency::exist(currencyCode))
        #                    {
        #                        CustTable.Currency  = currencyCode;
        #                    }
        #                    CustTable.insert();
        #                }
        #            }
        #        }
        #        first           = true;
        #    }
        #}
        #
      ENDSOURCE
      SOURCE #unpack
        #public boolean unpack(container _packedClass)
        #{
        #    Version version = runbase::getVersion(_packedClass);
        #
        #    switch (version)
        #    {
        #        case #CurrentVersion:
        #            [version, #CurrentList] = _packedClass;
        #            break;
        #
        #        default:
        #            return false;
        #    }
        #
        #    return true;
        #}
        #
      ENDSOURCE
      SOURCE #description
        #client server static ClassDescription description()
        #{
        #    return "Import Customer Data";
        #}
        #
      ENDSOURCE
      SOURCE #main
        #static void main(Args _args)
        #{
        #    ImportDataCSV import = new ImportDataCSV();
        #    ;
        #
        #        if (import.prompt())
        #            import.run();
        #}
        #
      ENDSOURCE
    ENDMETHODS
  ENDCLASS
***Element: END

No comments:

Post a Comment