runAs Function

Applies To: Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012

Enables the caller to run an X++ method in the security context of another user. This function is most often used with batch processing.

container runAs(
    str userId,
    int classId,
    str staticMethodName
    container params,
    str company,
    str language,
    str partition





The Microsoft Dynamics AX user to be impersonated.


The class to be invoked in the impersonated session.


The class method to be invoked in the new user context.


The parameters to be passed to the method; optional.


The company selected for the impersonated session; optional.


The language selected for the impersonated session; optional.


The partition key, of the type returned by the getCurrentPartition function; optional.

Return Value

The container that holds the return value or values, if any, of the method called by the runAs function.


This function makes it possible to run code as another Microsoft Dynamics AX user. This presents a security threat. Therefore, this function runs under Code Access Security. Calls to this function on the server require permission from the RunAsPermission Class. Each use of this API should be threat-modeled. If a security vulnerability is discovered, validate input to this API.

The debugger might ignore breakpoints that are located in a method that is called by using the runAs function.

X++ code that is executed by the runAs function must run as .NET Framework CIL. If CIL has not been generated for the target static method, an error message indicates that the method is not found.

The PartitionKey system type is the exact type of the partition parameter. PartitionKey is a string with a maximum length of 8 characters. The partition parameter was added in Microsoft Dynamics AX 2012 R2.


The following example calls the runDueDateEventsForUser method in the EventJobDueDate class. The code runs in the security context of a Microsoft Dynamics AX user. Run this code by applying it to a method in a new class.

    server static public void Main(Args _args)
        RunAsPermission perm;
        UserId          runAsUser;
        SysUserInfo     userInfo;
        userInfo = SysUserInfo::find();
        runAsUser = userInfo.Id;
        perm = new RunAsPermission(runAsUser);
        runAs(runAsUser, classnum(EventJobDueDate), "runDueDateEventsForUser");

See also



Announcements: To see known issues and recent fixes, use Issue search in Microsoft Dynamics Lifecycle Services (LCS).