Friday, November 20, 2020

Import data from CSV file - JOB AX 2012

 static void ImportfromCSV(Args _args)

{

    TTC_AppOfPF     appOfPF;

    Dialog          dialog  = new Dialog();

    DialogField     dialogField;

    AsciiIo         importFile;

    str             filePath,fileNameOnly;

    filetype        type;

    container       record;

    str             Delimiter = ",";

    int             totalRecords;


    dialogField=dialog.addField(extendedTypeStr(FilenameOpen),"Select File","Select file to import");

    dialog.caption("Import Worker's File");

    dialog.filenameLookupFilter(['csv','*.csv']);

    if(!dialog.run())

        return;

    [filePath, fileNameOnly, type] = fileNameSplit(dialogField.value());

    importFile = new AsciiIo(dialogField.value(), 'R');

    if((!importFile) || (importFile.status() != IO_Status::Ok))

    {

        warning("Error in opening import file");

        throw(Exception::Error);

    }

    importFile.inFieldDelimiter(Delimiter);

    if((!importFile) || (importFile.status() != IO_Status::Ok))

    {

        warning("Error in opening log file");

        throw(Exception::Error);

    }

    try

    {

        ttsbegin;

        record = importFile.read();

        while(importFile.status() ==  IO_Status::Ok)

        {

            record = importFile.read();

            if(!record)

                break;

            totalRecords = totalRecords + 1;

            appOfPF.clear();

            appOfPF.Worker                  = conPeek(record,1);

            appOfPF.PersonnelNumber         = conPeek(record,2);

            appOfPF.PFAccountNum            = conPeek(record,3);

            appOfPF.insert();

        }

        ttscommit;

    }

    catch(Exception::Error)

    {

        Throw(Exception::Error);

    }

    info(strFmt("Total read records = %1",totalRecords));

}

Wednesday, November 18, 2020

Common table in Ax 2012

Common : The system table Common is the base for all tables. The table contains no data, and the table can be compared with a temporary application table. Common is used like the base type anytype. You can set any table equal to the table Common. This is useful if you need to transfer a table as a parameter and not knowing the exact table before execution time

static void DataDic_Common(Args _args)

{

Common common;

CustTable custTable;

;

common = custTable;

if (common.tableId == tablenum(custTable))

{

while select common

{

info(common.(fieldnum(custTable, name)));

}

}

}

The example above is initializing Common with CustTable. A check is made to assure that Common is now equal to CustTable. All records from CustTable are looped, and the customer name is printed in the InfoLog. Notice, the system function fieldnum() is used to get the field name printed. If you do not know the fieldname at runtime, you can enter the field id in parentheses instead.