Sunday, November 1, 2020

How to get Date or Time from UTCDateTime in ax 2012 using X+

 

Hi guys, Today we see how to get date or Time from UTCDateTime in ax 2012 using X++. 


Run the below job.


static void dateFunctions(Args _args)

{

    CustTable       custable;

    int             daysdiff;

    Date            _Currentdate;

    utcDateTime     _UtcStartPeriod;

    utcDateTime     _UtcEndPeriod;

    utcDateTime     dateTime;

    date            dateInUserTimeZone;

    TimeOfDay       timeInUserTimeZone;

    AccountNum      _accountNum = '456';

    

    select custable where custable.AccountNum == _accountNum;

    

    // Convert normal date into utcDateTime

    _currentdate=today();

   _UtcStartPeriod =  DateTimeUtil::newDateTime(_currentdate,0);


    // Convert utcDateTime into Date

    dateInUserTimeZone = DateTimeUtil::date(DateTimeUtil::applyTimeZoneOffset(custable.modifiedDateTime, DateTimeUtil::getUserPreferredTimeZone()));

    timeInUserTimeZone = DateTimeUtil::time(DateTimeUtil::applyTimeZoneOffset(custable.modifiedDateTime, DateTimeUtil::getUserPreferredTimeZone()));

    //dateTime = DateTimeUtil::newDateTime(dateInUserTimeZone, timeInUserTimeZone, DateTimeUtil::getUserPreferredTimeZone());

    

    daysDiff =  dateInUserTimeZone - today();


    info(strfmt("today date - %1",today()));

    info(strfmt("cust account date - %1",dateInUserTimeZone));

    info(strfmt("cust account time - %1",timeInUserTimeZone));


    info(strfmt("days difference - %1",daysDiff));

}

Output:





Keep Daxing!!

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!!