Tuesday, June 21, 2022

How to get messages from Infolog through x++ in Dynamics 365 FO


Error Handling is one of the very good practices to used especially when we are using integration of Dynamics 365 Fin Ops with other applications.


When we encounter these errors in an infolog the best way to get those error messages is through two most important classes : - 


SysInfologEnumerator  

SysInfoLogMessageStruct


Lets see throw below code snippet how these classes help us to achieve our goal : - 



class GetInfologMessages
{
    public static str getErrorStr()
    {
        SysInfologEnumerator         enumerator;

        SysInfologMessageStruct     msgStruct;

        Exception                              exception;

        str                                          error;   

        enumerator      =   SysInfologEnumerator::newData(infolog.copy(1,infolog.num()));

        while(enumerator.moveNext())
        {
            msgStruct   =   new SysInfologMessageStruct(enumerator.currentMessage());

            exception   =   enumerator.currentException();

            error       =   strfmt("%1 %2", error, msgStruct.message());
        }

        return error;
    }

    public static utcdatetime getErrorDatetime()
    {

        SysInfologEnumerator        enumerator;

        SysInfologMessageStruct     msgStruct;

        Exception                   exception;

        str                         errormsg;

        utcdatetime                 errordatetime;

        enumerator      =   SysInfologEnumerator::newData(infolog.copy(1,infolog.num()));

        while (enumerator.moveNext())
        {
            msgStruct   =   new SysInfologMessageStruct(enumerator.currentMessage());

            exception   =   enumerator.currentException();

            errormsg       =   strfmt("%1 %2", errormsg, msgStruct.message());
        }

        if(errormsg!='')
        {
            errordatetime = DateTimeUtil::getSystemDateTime();
        }

        return errordatetime;
    }
}


That's all for now. Please let us know your questions or feedback in comments section !!!!

No comments:

Post a Comment

How to reverse Free Text Invoice Voucher entries without Dialog

 Hey Folks ,  This blog post is in continuation of the previous post for Reversing Free Text Invoice Voucher entries with Dialog. Only diffe...