I need to run an SOQL command, as admin just like system.runAs(u) in controller.

Is there a way to do this.

  • Can you describe your reason for needing to run code with such elevated credentials?
    – Mark Pond
    Commented Dec 2, 2013 at 5:50
  • Hey apex run in system context so it does NLT depend whether u run as admin or not. If you are having some prob. In running or require extra permission be sure to check with sharing keyword should not be there
    – user3170
    Commented Dec 2, 2013 at 8:26

3 Answers 3


To run a query as "an administrator", use the "without sharing" keyword within a class:

public without sharing class Utility {
    public static Account[] queryAccounts() {
        return [SELECT Id, Name, Industry FROM Account LIMIT 100];

While you are still running the query as the current user, the current user's sharing is ignored, which means that the query will execute as if the user were an administrator.

Also note that within triggers, the code runs by default "without sharing", so it is generally not necessary to run "as administrator" unless you're using utility classes (such as the example here).

  • I have the same issue and the answer from sfdcfox works beautiful. I have a class that when is executed by admin bring all the data but for others user just bring their own information. With without Sharing at the beginning of the class the current user's sharing is ignored. Thanks to sfdcfox for the solution...
    – MANUELAN00
    Commented Aug 18, 2015 at 2:43

There is NO way to do this outside of test methods and for good reason.

In Test Methods:

By default the code will likely be running as an admin user. What you can do though is grab the profile ID for the 'System Administrator' profile, insert a new user using that profile, then run as the new user.

Id profileId = [select Id from UserProfile where Name = 'System Administrator' limit 1].Id

User u = new User();
// fill in required fields (well documented)
u.UserProfileId = profileId; // think this is the right field name, double check
insert u;

  • but how to run as new user?
    – sfdc
    Commented Oct 28, 2013 at 1:16
  • I got an error. System.runAs can only be used within a test method:
    – sfdc
    Commented Oct 28, 2013 at 1:51
  • 1
    Oh sorry. I assumed you meant in a test method. There is no way to run code as another user otherwise as that would potentially be a huge security breach.
    – Matt Lacey
    Commented Oct 28, 2013 at 5:16
  • 5
    do you really need to run as another user, or just with System privileges ? how about using without sharing ?
    – ManSpan
    Commented Oct 28, 2013 at 10:02

Yes as the error suggest, the


method can be used only in Test Classes. As @LaceySnr suggested, all APEX code run with "View All" and "Modify All" permissions.

If your apex classes have "With Sharing" Keyword, then all the Sharing rules for logged-in user apply.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .