function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

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?  ^^^




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

insert u1;

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


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

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

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

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

insert opptys;

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






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



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