Tuesday 17 December 2019

Date to DateTime conversion and comparison

If you want just to convert specified date to dateTime, and it should be beginning of this day (date time), you can use

DateTimeUtil::newDateTime(mkDate(17, 12, 2019), 0);
//17th of December 2019

Or just
DateTimeUtil::newDateTime(systemdateGet(), 0);
//current system date

But if you want to have maximum time from that date, so it means hour 23:59:39
DateTimeUtil::newDateTime(mkDate(17, 12, 2019), 86400);

As 86400 is maximum time value for given date (24 hours * 60 minutes * 60 seconds)

In Dynamics 365FO, systemDateGet() is usable but obsolete, and you'll get Best Practice warning.
You should rather use
DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone())



Wednesday 11 December 2019

How to change Created by on batch job. Change user on which batch job will be executed

It's not good idea to create batch jobs using standard user account, because such user may be disabled after some time, and there will be nobody with knowledge, why do some batches (even very old) just  not work.

But, if we have plenty of such batches, and we want them to be executed on some service account or on admin account, we don't have to recreate them.

There are two tables which keep information which user will execute those batches:
- Batch
- BatchJob

You have to change fields ExecutedBy and CreatedBy in Batch table, and only CreatedBy in BatchJob table

You can do that manually from Microsoft SQL Server Management Studio, or just writing a script

begin transaction

update BATCHJOB
set CREATEDBY='SomeServiceAccount'
where CREATEDBY='admin_10'

update BATCH
set EXECUTEDBY='SomeServiceAccount', CREATEDBY='SomeServiceAccount'
where CREATEDBY='admin_10'

commit transaction

Thursday 5 December 2019

How to build complex query range in query

Sometimes there is a need to query some records using OR and put it to a query range
For example: we need to query records which are from the past and from the future.

Below, you can find an example of such query on table called just Table1
There is no date effective property on the table, so fields ValidFrom and ValidTo are just Date type fields created on that table.
As you can see all range has been built on ValidFrom field, even when it touches also ValidTo.


static void Job21(Args _args)
{
    Query                   query = New Query();    
    QueryRun                queryRun;
    QueryBuildDataSource    qbds;
    str                     range;
    Table1                  table1;
    
    qbds = query.addDataSource(tableNum(Table1)); 
      
    qbds.addRange(fieldNum(Table1, ValidFrom));
    
    range = strFmt('((%1.%2 < %3) && (%4.%5 < %6)) || ((%7.%8 > %9) && (%10.%11 > %12))',
        qbds.name(),
        fieldStr(table1, ValidFrom),
        date2StrXpp(today()),
        qbds.name(),
        fieldStr(table1, ValidTo),
        date2StrXpp(today()),
        /// after or
        qbds.name(),
        fieldStr(table1, ValidFrom),
        date2StrXpp(today()),
        qbds.name(),
        fieldStr(table1, ValidTo),
        date2StrXpp(today()));    
    
    qbds.addRange(fieldNum(Table1, ValidFrom)).value(range);    
}

Tuesday 3 December 2019

Progress bar

How to create a progress bar from X++



static void Job39(Args _args)
{
    SalesLine               salesLine;
    Counter                 i;
    SysOperationProgress    progressBar = new SysOperationProgress();    
    #avifiles

    select count(RecId) from salesLine;
    
    progressBar.setCaption('Searching...');
    progressBar.setAnimation(#AviUpdate);    
    progressBar.setTotal(salesLine.recId);    
    
    while select salesLine
        where !salesLine.RemainInventFinancial
            && !salesLine.RemainSalesPhysical
    {
        i++;
        
        progressBar.setText(strfmt('Row %1', i));
        progressBar.setCount(i, 1);        
    }

}