Luntri
Luntri

Reputation: 620

Dynamics AX - Adding tables to DatabaseLog programatically in AX 2009

I'm looking for a way to enable logging changes for certain tables.
I have tried and tested adding tables to database log programatically, but with various success so far - sometimes it works sometimes it doesn't (mostly it does not) - it seems simply inserting rows into DatabaseLog table doesn't quite do the trick.

What I have tried:
Adding row with proper tableId, fieldId, logType and .
Domain had been assigned as 'Admin', main company, empty field and subcompanies with the same result.
I have created class that handles inserts, main two functions are:

public static void InsertBase(STR tableName, domainId _domain='Admin')
{
    //base logging for insert, delete, uptade on fieldid=0
    DatabaseLog DBDict;
    TableId _tableId;
    DatabaseLogType _logType;
    fieldId _fieldId =0;
    List logTypes;
    int i;
    ListEnumerator enumerator;
    ;

    _tableId= tableName2id(tableName);

    logTypes = new List(Types::Enum);

    logTypes.addEnd(DatabaseLogType::Insert);
    logTypes.addEnd(DatabaseLogType::Update);
    logTypes.addEnd(DatabaseLogType::Delete);
    logTypes.addEnd(DatabaseLogType::EventInsert);
    logTypes.addEnd(DatabaseLogType::EventUpdate);
    logTypes.addEnd(DatabaseLogType::EventDelete);

    enumerator = logTypes.getEnumerator();
    while(enumerator.moveNext())
    {
        _logType = enumerator.current();

        select * from dbdict where
                dbdict.logTable==_tableId && dbdict.logField==_fieldId
                && dbdict.logType==_logType;
        if(!dbDict) //that means it doesnt exist
        {
            dbdict.logTable=_tableId;
            dbdict.logField=_fieldId;
            dbdict.logType=_logType;
            dbdict.domainId=_domain;
            dbdict.insert();
        }
    }
    info("Success");
}

and the method that lists every single field and adds as logType::Update

public static void init(str TableName, DomainId domain='Admin')
{
    DatabaseLogType logtype;
    int i;
    container kk, ll;
    DatabaseLog dblog;
    tableid _tableId;
    fieldid _fieldid;
    ;

    logtype = DatabaseLogType::Update;
    //holds a container of not yet added table fields to databaselog
    kk = BLX_AddTableToDatabaseLog::buildFieldList(logtype,TableName);
    for(i=1; i <= conlen(kk);i++)
    {
        ll = conpeek(kk,i);
        _tableid = tableName2id(tableName);
        _fieldid = conpeek(ll,1);
        info(strfmt("%1 %2", conpeek(ll,1),conpeek(ll,2)));

        dblog.logType=logType;
        dblog.logTable = _tableId;
        dblog.domainId = domain;
        dblog.logField =_fieldid;
        dblog.insert();
    }
}

result: Select * from databaselog where logtable=662

What am I missing ?

@EDIT with some additional info
Does not work for SalesTable and SalesLine, WMSBillOfLading.
I couldn't add log for SalesTable and SalesLine by using wizard in administration panel, but my colleague somehow did (she has done exactly the same things as me). We also tried to add log to various other tables and we often found out that she could while I could not and vice versa (and sometimes none managed to do it like in case of WMSBillOfLading table).

The inconsistency of this mechanism is what drove me to write this code, which I hoped would solve all the problems.

Upvotes: 2

Views: 995

Answers (1)

DAXaholic
DAXaholic

Reputation: 35338

After doing your setup changes you probably have to call

SysFlushDatabaseLogSetup::main();

in order to flush any caches.
This method is also called in the standard AX code in the form method SysDatabaseLogTableSetup\Methods\close and in the class method SysDatabaseLogWizard\doRun.

Upvotes: 5

Related Questions