Thursday, February 1, 2024

Handling update conflict and capture standard error message to throw error to end user in D365 F&O

We can use follow sample code to handle update conflict in case of exceptions and then Method to capture the standard error and send it the particular catch of the method.

Hope this helps!

public void runOperation()

    {

        #OCCRetryCount

        try

        {

            ttsbegin;

            //Do something insert/update

            ttscommit;


        }

        catch(Exception::UpdateConflict)

        {

            if (appl.ttsLevel() == 0)

            {

                if (xSession::currentRetryCount() >= #RetryNum)

                {

                    throw Exception::UpdateConflictNotRecovered;

                }

                else

                {

                    retry;

                }

            }

        }

        catch

        {

            info(strFmt(this.getErrorStr()));

        }

    }


    /// <summary>

    /// Method to capture the standard error and send it the perticular catch of the method.

    /// </summary>

    /// <returns>error</returns>


    public str getErrorStr()

    {

        SysInfologEnumerator    enumerator;

        SysInfologMessageStruct msgStruct;

        Exception               exception;

        str                     error;


        enumerator = SysInfologEnumerator::newData(infolog.cut());


        while (enumerator.moveNext())

        {

            msgStruct = new SysInfologMessageStruct(enumerator.currentMessage());

            exception = enumerator.currentException();

            error = strfmt("@SYS82282"+ ' '+"@MCR26302", error, msgStruct.message());

        }

        return error;

    }


#D365F&O, #X++, #Exceptionhandling, #UpdateConflict

No comments:

Post a Comment