+ Start a Discussion

RunAs function problem - please help

Good morning,
I wrote a testMethod, in which I use a runAs user, like:
                Profile pr2 = [select id from profile where name='Standard User'];
                User u = new User(Manager=OTHERTESTUSER,alias = 'standt', email='standarduser@testorg.com',emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',localesidkey='en_US', profileid = pr2.Id,timezonesidkey='America/Los_Angeles', username='standarduser@testorg.com');


I want to set an other TestUser as the Manager of RunAs User....is that possible ?
I have attempted to create and insert a new User before the System.runAs statement, but that works somehow not..hmm.
Thanks a lot in advance for help!
What kind of error are you getting?  I have created test users with various profiles and use the runAs to execute test cases many times.

I assume you want to specific the User.ManagerId field to specify the "Manager".
If so, I was able to insert 2 user records (setting the ManagerId of the 2nd one with the User Id of the 1st one), then I set the runAs().
Here is the code:

public static testMethod void testRunAs() {

Profile p = [select id from profile where name='Standard User']; User u_manager = new User(alias = 'standt', email='standarduser@testorg.com', emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US', localesidkey='en_US', profileid = p.Id, timezonesidkey='America/Los_Angeles', username='standarduser@testorg.com'); insert u_manager;
User u = new User(ManagerId=u_manager.id,alias = 'standt2', email='standarduser2@testorg.com',
emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US', localesidkey='en_US',
profileid = p.Id, timezonesidkey='America/Los_Angeles', username='standarduser2@testorg.com'); insert u; System.runAs();
System.debug('Current User: ' + UserInfo.getUserName());
System.debug('Current Profile: ' + UserInfo.getProfileId());



Thanks for your replies !

I have tested the code (...insert u_manager; ...) and I get still this error message:

But I can create an User manual, so I don't understand this message...what's the matter?

Thx, Peter

My guess is that you are using a developer instance, which only supports a max of 2 users.  You have one as the admin, and the 1st user you create is the second, when you try to create the 3rd, you get the error.
I would recommend for testing purposes, you make yourself the manager of the new user you create.  Then your test should work.
Thx JimRae, I have had the same thought right now and it works, my realization:
//use current User as Manager of 'testUser'
User  cu = [select Id from User where Id =:UserInfo.getUserId()];
//create testUser
Profile pr1 = [select id from profile where name='Standard User'];
User testUser = new User(Manager=cu,alias = 'standt', email='standarduser@testorg.com',emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',localesidkey='en_US', profileid = pr1.Id,timezonesidkey='America/Los_Angeles', username='standarduser@testorg.com');
//run tests as testUser



What if the number of actual production licenses have reached the max and I have to deploy codes and the test methods that create a new user for testing, then what's my alternative?


I can change the test codes to retrieve an actual user with the right profile I want, but that will change lots of test codes. It's kind of lame that test codes, which have been clearly annotated @isTest and testMethod for testing, invoke Salesforce license limit.


Don't tell me that I can't deploy without getting a new license in order to pass the test code coverage.


Please help.


I met an interesting problem with RunAs() in my test method for a trigger.


If I use  Test.startTest() and Test.stopTest( ) together with RunAs() function, I got the error: System.Exception: Too many System.runAs() invocations:1


When I use RunAt() alone without  Test.startTest() and Test.stopTest( ), ther is no error at all.


Can anyone explain why? 




Always ThinkinAlways Thinkin

I just hit the same problem trying to deploy to production: License Limit Exceeded. I had to temporarily deactivate a real user so that I could get my code to deploy since my test method creates a user to fulfull the RunAs function.


Have you hit this too? Vote up this idea so Salesforce gets the message:



Ron HessRon Hess

if your search found this thread, it looks like this idea was delivered recently.