web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Problem with RunAs, fails with object does not have method.

(0) ShareShare
ReportReport
Posted on by 4,075

I am having a wierd problem with RunAs in D365FO, when i try to call the method with runAs(runAsUser, classnum(TestRunAs), staticMethodStr(TestRunAs, d365User), parm) it fails with:
TestRunAs object does not have method 'd365User'.

But I have no problem calling the method normally using TestRunAs::d365User directly.

Here is some test code I made to show the problem.

class TestRunAs
{
    public static void main(Args _args)
    {
        container test = conNull();
        container result = conNull();

        result = TestRunAs::d365User(test);  // This Works.
        TestRunAs::d365UserRunAs("Admin");      // This fails with "TestRunAs object does not have method 'd365User'."
    }

    public static server container d365User(container _param)
    {
        container ret = [curUserId()];

        return ret;
    }

    public static server str d365UserRunAs(UserId _userId)
    {
        RunAsPermission perm;
        UserId          runasUser = _userId;
        container       parm = conNull();
        container       result          ;
        str             userId;
        
        perm = new RunAsPermission(runAsUser);
        perm.assert();

        result = runAs(runAsUser, classnum(TestRunAs), staticMethodStr(TestRunAs, d365User), parm);
        CodeAccessPermission::revertAssert();
        userId = conPeek(result, 1);

        return userId;
    }
}

I have the same question (0)
  • Martin Dráb Profile Picture
    237,994 Most Valuable Professional on at

    I can confirm that it behaves the same in my environment.

    I don't know what's wrong, but just for information, I tried to call the method in a few other ways, which all work:

    • SysDictClass.callStatic()
    • SysOperationSandbox::callStaticMethod()
    • runAsync()

    Only runAs() has a problem with finding the method.

  • Verified answer
    Martin Dráb Profile Picture
    237,994 Most Valuable Professional on at

    It seems it doesn't like conNull() as the fourth argument. It works if I provide an actual container:

    result = runAs(runasUser, classnum(RunnableClass1), staticMethodStr(RunnableClass1, d365User), conNull()); // Error
    result = runAs(runasUser, classnum(RunnableClass1), staticMethodStr(RunnableClass1, d365User), ['']); // OK

  • Rudi Hansen Profile Picture
    4,075 on at

    Wow that was not exactly what I had imagined that the problem was.

    Thanks for the help. (Now wonder if I am allowed to report this to MS in some way.....)

    EDIT : No we do not have a support plan for D365FO on LCS, witch by the way is the last step in the reporting process, so thanks MS for wasting my time trying to help you improve your software.
    Sorry about the rant but since I have absolutely no way to scream at MS, this will have to do.

  • Martin Dráb Profile Picture
    237,994 Most Valuable Professional on at

    I mentioned in the Insider forum. Let's see what happens - sometimes things are ignored there completely, sometimes they're acted upon in no time.

  • Martin Dráb Profile Picture
    237,994 Most Valuable Professional on at

    Here is a reply from Peter Villadsen:

    I looked into it, and the behavior is by design, and it does make sense when you think about it. Specifying an empty container (conNull()) means: "I want to call the given method with NO arguments". The system is quite correct in not finding a method without arguments matching the name provided. You can argue that it would be more natural to always look for a method with a container argument, and then just pass the empty container in this case, but that is not the behavior we have. We will not make any changes for this, since it will likely break more scenarios than it is worth.

    And my reaction.

    Hi Peter, I see your point. What I (and probably not only I) assumed is that runAs() is always used with methods accepting and returning a container. It seems that there are two cases - it can either accept no parameters or one container.

    Another confusing thing is that this behavior isn't consistent with methods like runAsync() or SysOperationSandbox::callStaticMethod(). Calling the method via runAsync() (for example) with conNull() works, doing the same via runAs() fails. It's true that the container parameter is mandatory for runAsync() and SysOperationSandbox::callStaticMethod(), which suggests that they have different meaning than the optional parameter in runAs(), but it's such a subtle difference that it's easy to overlook it and its implications.

    I agree that changing the behavior isn't worth it. But there is a place where a difference can be made - in documentation of runAs() (https://docs.microsoft.com/en-us/dynamics365/fin-ops-core/dev-itpro/dev-ref/xpp-session-run-time-functions).

    By the way, consider getting into the Insider program, to be able to participate in such discussions directly.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Abhilash Warrier Profile Picture

Abhilash Warrier 669 Super User 2025 Season 2

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 449 Super User 2025 Season 2

#3
Martin Dráb Profile Picture

Martin Dráb 384 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans