+ Start a Discussion
savvyboardersavvyboarder 

System.Exception: Too many System.runAs() invocations: 1

I'm curious if anybody knows why I'm getting the error in the subject of this thread?  ^^^

 

 

test.starttest();

Profile p = [SELECT Id FROM profile WHERE name='API'];
User u1 = new User(alias = 'newUser', email='user@domain.com',
emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id,
timezonesidkey='America/Los_Angeles', username='user@domain.com');

insert u1;

system.debug('###### the user is: ' + u1.Id);

System.runAs(u1)
{

List<Opportunity> opptys = new List<Opportunity>();

Opportunity o1 = new Opportunity();
o1.Name = 'Test Oppty 1';
o1.CloseDate = Date.today();
o1.StageName = 'Reservation';
o1.Rental_Rate__c = 100;
opptys.add(o1);

Opportunity o2 = new Opportunity();
o2.Name = 'Test Oppty 2';
o2.CloseDate = Date.today();
o2.StageName = 'Appointment';
opptys.add(o2);

Opportunity o3 = new Opportunity();
o3.Name = 'Test Oppty 3';
o3.CloseDate = Date.today();
o3.StageName = 'Lead';
opptys.add(o3);


insert opptys;

system.debug('##### the opportunities are: ' + opptys);
system.debug('##### created by: ' + opptys[0].CreatedById);

}

test.stopTest();

 

 

 

Message Edited by savvyboarder on 06-10-2009 02:44 PM
wesnoltewesnolte

Hey

 

Salesforce will tell you that you cannot use a RunAs from within an @future method. In my experience if you remove the Test.startTest() and Test.stopTest() it should work, although there may be other errors that you receive.. if you do get additional errors, remove any asserts in your code and try again.

 

The strange thing is that this solution(the one you've implemented) is suggested as a fix for the bug in @future i.e. it doesn't truly run in another context. I'm currently stuck because of this workaround for a bug that is limited by a Governor limit:P

 

Cheers,

Wes