30 June 2020

D365FO: a case of misleading error message

Recently I made a simple test class to try out threading in X++ :


class CallStaticMethodClass
{ 
    public static void main(Args _args)
    {
        UtilElementName methodNameToRun = staticMethodStr(CallStaticMethodClass, myThreadMethod);
        xGlobal::runAsync(classNum(CallStaticMethodClass), methodNameToRun, conNull());
 
        Info("Method run completed successfully");
    }
 
    public static void myThreadMethod()
    {
        //Do something
    } 
}

To my surprise, when the class was executed, the following exception appeared:


Error executing code: CallStaticMethodClass object does not have method 'myThreadMethod'

The solution was to change myThreadMethod(), so it will return container instead the void. In this case the method would be found and correctly executed on the background. Updated class code:



class CallStaticMethodClass
{ 
    public static void main(Args _args)
    {
        UtilElementName methodNameToRun = staticMethodStr(CallStaticMethodClass, myThreadMethod);
        xGlobal::runAsync(classNum(CallStaticMethodClass), methodNameToRun, conNull());
 
        Info("Method run completed successfully");
    }
 
    public static container myThreadMethod()
    {
        container con;

        //Do something

        return con;
    } 
}

PS: you could also use xGlobal::runAsyncWithObjectCallback() method instead of 
xGlobal::runAsync() if your thread should report back.

No comments:

Post a Comment