Sunday, November 1, 2020

Import General Journals through Excel in AX 2012 X++


 Hi guys, Today we see how to Import General Journals through Excel in Ax 2012 using Excel.


Run the below job.

static void JournalImport(Args _args)

{

    Dialog                  _dialog;

    DialogField             _file;

    SysExcelApplication     application;

    SysExcelWorkbooks       workbooks;

    SysExcelWorkbook        workbook;

    SysExcelWorksheets      worksheets;

    SysExcelWorksheet       worksheet;

    SysExcelCells           cells;

    COMVariantType          type;

    Name                    name;

    FileName                filename;

    LedgerJournalACType     LedgerJournalACType;



    AxLedgerJournalTable    axLedgerJournalTable ;

    AxLedgerJournalTrans    axLedgerJournalTrans;


    container               accPattern,offSetPattern;

    int                     row = 1;


    str                     _Name;

    str                     _Description;

    AmountCurDebit          _Debit;

    str                     _LedgerDimension;

    str                     _OffsetAccountType;

    str                     _AccountType;

    str                     _OffsetLedgerDimension;



    axLedgerJournalTable = new AxLedgerJournalTable();


    axLedgerJournalTrans = new AxLedgerJournalTrans();


    _dialog = new Dialog("Import Data From Excel");

    _dialog.addText("Select file:");


    _file   = _dialog.addField(ExtendedTypeStr("FilenameOpen"));

    _dialog.run();


    if (_dialog.closedOK())

    {


        info(_file.value() );


    application = SysExcelApplication::construct();

    workbooks   = application.workbooks();

    //specify the file path that you want to read

    filename   =_file.value(); //ExcelSheet File Name

    try

    {

        workbooks.open(filename);

    }

    catch (Exception::Error)

    {

         throw error('File cannot be opened');

    }


    workbook    = workbooks.item(1);

    worksheets  = workbook.worksheets();

    worksheet   = worksheets.itemFromNum(1); //Here 1 is the worksheet Number

    cells       = worksheet.cells();

    do

    {

        row++;

        _Name                       = cells.item(row, 1).value().bStr();

        _Description                = cells.item(row, 2).value().bStr();

        _Debit                      = cells.item(row, 3).value().double();

        _OffsetAccountType          = cells.item(row, 5).value().bStr();

        _AccountType                = cells.item(row, 6).value().bStr();



        axLedgerJournalTable.parmJournalName(_Name);


        axLedgerJournalTable.save();


        axLedgerJournalTrans.parmJournalNum(axLedgerJournalTable.ledgerJournalTable().JournalNum);


        axLedgerJournalTrans.parmTransDate(systemDateGet());

        axLedgerJournalTrans.parmCurrencyCode('USD');

        axLedgerJournalTrans.parmTxt(_Description);

        axLedgerJournalTrans.parmAmountCurDebit(_Debit);

        axLedgerJournalTrans.parmExchRate(Currency::exchRate('USD'));

        axLedgerJournalTrans.parmAccountType(str2enum(LedgerJournalACType,_AccountType));



        accPattern = ['110110','110110',2,'BusinessUnit',"001","Department","022"];

        axLedgerJournalTrans.parmLedgerDimension(AxdDimensionUtil::getLedgerAccountId(accPattern));



        offSetPattern = ['112140',"112140",2,'BusinessUnit','003',"Department","024"];

        axLedgerJournalTrans.parmOffsetLedgerDimension(AxdDimensionUtil::getLedgerAccountId(offSetPattern));


        axLedgerJournalTrans.save();


        type = cells.item(row+1, 1).value().variantType();

    }


        while (type != COMVariantType::VT_EMPTY);

        application.quit();

        info(strFmt("Journal %1 created", axLedgerJournalTable.ledgerJournalTable().JournalNum));

        info("Data is Imported");

    }

    }


Keep Daxing!!

No comments:

Post a Comment