Thursday, August 11, 2016

Customize Database Log Table in AX 2012

//Customize Database Log Table in AX 2012


Class--> FCCLog
//Create Table FCCLogTable to track
//Create Enum FCCEventTypes -- (Ex: Insert,Update,Delete)
// Create Enum FCCLogTransaType -- (ex: Transaction Type (worker,Dirparty,transaction,master)
//Class declaration
class FCCLog
{
    FCClLog       Log;
    FCCEventType        _evType;
    FCCLogTransType     _trType;
    Common              oldrec,newrec;
    HcmWorkerRecId      workerrec;
    TableId             table;
    RefRecId            refRec;
    real        oldv,newv,diff;
}
//Insert Log Table
void insertLog(FieldName fld,str oldVal, str newVal,RefRecId rec)
{
    SysDictTable dictTable = new SysDictTable(table);
    ;
    if(_evType!= FCCEventType::Update || oldVal != newVal)
    {
        Log.clear();
        Log.HcmWorkerRecId = workerrec;
        Log.workerName = HcmWorker::find(workerrec).name();
        Log.Diff = diff;
        Log.FieldNm = fld;
        Log.LogTransType = _trType; //Table name
        Log.EventType = _evType;  // insert,Delete,Update
        Log.OldValue = oldVal;
        Log.NewValue = newVal;
        Log.Reference = rec;
        Log.Table = table;
        log.tablenm = dictTable.label();
        Log.User = curUserId();
        Log.transDate = today();
        Log.FCCTime = DateTimeUtil::time(DateTimeUtil::utcNow());
        Log.insert();
    }
}
//new method
void new(Common rec,FCCEventType evType)
{
    HcmWorker       worker;
    DirPerson   DirPartyTable;
    ;
    _evType = evType;
    switch (rec.TableId)
    {
        case tablenum(HcmWorker):
            _trType = FCCLogTransType::EmployeeDetails;
            worker = rec;
            workerrec = worker.recid;
            table = worker.TableId;
            refrec = rec.RecId;
            this.LogTable(rec);
            break;
        case tablenum(DirPartyTable):
            _trType = FCCLogTransType::EmployeeDetails;
            DirPartyTable = rec;
            worker = HcmWorker::findByPerson(DirPartyTable.RecId);
            workerrec = worker.RecId;
            if(workerrec)
            {
                table = DirPartyTable.TableId;
                refrec = rec.RecId;
                this.LogTable(rec);
            }
            break;
        default:
            break;
    }
}
//Log Table find
void LogTable(Common rec)
{
    SysDictTable dictTable = new SysDictTable(rec.TableId);
    SysDictField dictField;
    FieldId fieldId = dictTable.fieldNext(0);
    str oldVal,newVal;
    ;
    try
    {
        oldrec = rec.orig();
        newrec = rec;
        if(_evType == FCCEventType::Update)
        {
            while (fieldId)
            {
                dictField = dictTable.fieldObject(fieldId);
                if (dictField.isSql() && !dictField.isSystem() && dictField.arraySize() == 1
                    && dictfield.allowEdit() && dictfield.baseType() != Types::Container)
                {
                    oldVal =strfmt("%1",oldrec.(fieldId));
                    newVal = strfmt("%1",rec.(fieldId));
                    if(dictField.baseType() == Types::Int64 || dictField.baseType() == Types::Integer ||
                        dictField.baseType() == Types::Real)
                        {
                            diff = rec.(fieldId) - oldrec.(fieldId);
                        }
                    if(oldVal != newVal)
                    {
                        this.insertLog(dictField.label(),oldVal, newVal,rec.RecId);
                    }
                }
  else
  {
   oldVal =strfmt("%1",oldrec.(fieldId));
                     newVal = strfmt("%1",rec.(fieldId));
   this.insertLog(dictField.label(),oldVal, newVal,rec.RecId);
  }
                fieldId = dictTable.fieldNext(fieldId);
            }
        }
        else if(_evType == FCCEventType::Insert)
        {
            this.insertLog("","","",newrec.RecId);
        }
        else if(_evType == FCCEventType::Delete)
        {
            this.insertLog("","","",0);
        }
    }
    catch
    {
        return;
    }
}

//Adding Method in Table level
//delete,Update,Insert
new FCCLog(this,FCCEventTYpe::Delete);
new FCCLog(this,FCCEventTYpe::Insert);
new FCCLog(this,FCCEventTYpe::Update);

No comments:

Post a Comment

Convert Call stack to readable format in D365FO X++

//Input --container _xppCallStack = xSession::xppCallStack();  Public static str POL_formatXppCallStack(container _xppCallStack, int _skipFr...