Activity Forums Salesforce® Discussions What is System runAs in Salesforce?

  • Piyush

    Member
    September 10, 2019 at 4:39 am

    Hi Deepak,

    The system method runAs enables you to write test methods that change the user context to an existing user or a new user so that the user's record sharing is enforced.

  • Laveena

    Member
    September 10, 2019 at 6:37 am

    Hi Deepak,

    System runAs method can also be used  to perform mixed DML operations in your test by enclosing the DML operations within the runAs block. In this way, you bypass the mixed DML error that is otherwise returned when inserting or updating setup objects together with other sObjects.

    Thanks

  • Saddam

    Member
    September 10, 2019 at 9:35 am

    Hi Deepak,

    Generally, all Apex code runs in system mode, and the permissions and record sharing of the current user are not taken into account. The system method, System.runAs(), lets you write test methods that change user contexts to either an existing user or a new user. All of that user’s record sharing is then enforced. You can only use runAs in a test method. The original system context is started again after all runAs() test methods complete.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos