Thursday, December 12, 2024

Convert Call stack to readable format in D365FO X++

//Input

--container _xppCallStack = xSession::xppCallStack(); 


Public static str POL_formatXppCallStack(container _xppCallStack, int _skipFrames)

{

    str result = '';

    int startFrame = 1 + _skipFrames;


    // Each stack frame is four elements [Method, Line Number, Model Publisher, Model]

    for (int i = startFrame; i <= conLen(_xppCallStack); i += 4)

    {

        str methodName = conPeek(_xppCallStack, i);

        str lineNum = conPeek(_xppCallStack, i + 1);

        str publisher = conPeek(_xppCallStack, i + 2);

        str model = conPeek(_xppCallStack, i + 3);


        // Add a newline between stack frames

        if (i > 1)

        {

            result += '\n at';

        }

        

        // Model identifer

        if (publisher || model)

        {

            result += strFmt('[%1%2%3]', publisher, (publisher && model ? ':' : ''), model);

        }

        

        // Code

        result += strFmt('%1%2', methodName, (lineNum && lineNum != '0' ? ':' + lineNum : ''));

    }


    return result;

}

Convert Call stack to readable format in D365FO X++

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