You need to sign in to do that
Don't have an account?
Roger Wicki
runAs() query user object
Dear Community
I have a couple of apex handler classes which are executed on certain trigger actions. It so comes that the test classes of the individual apex classes do interfere with one another.
So I might have some code executed on an OpportunityLineItem insertion which updates the Opportunity. For the test class i need to create an test opportunity record which will also cause all my triggers on opportunity to run.
All of this is no problem yet. But here it comes: One of my Test classes uses the "System.runAs()" with different users to create test opportunities because depending on the user/profile/permission set assigned to the user, some code might execute differently.
Because this runAs method will create opportunities as different users and I have a trigger on Opportunity which executes a SOQL on the User object, the SOQL itself is executed as the user defined in the runAs method. Now for some unclear reason, I receive an error that there was no record to assign to a variable (see example)
I have a couple of apex handler classes which are executed on certain trigger actions. It so comes that the test classes of the individual apex classes do interfere with one another.
So I might have some code executed on an OpportunityLineItem insertion which updates the Opportunity. For the test class i need to create an test opportunity record which will also cause all my triggers on opportunity to run.
All of this is no problem yet. But here it comes: One of my Test classes uses the "System.runAs()" with different users to create test opportunities because depending on the user/profile/permission set assigned to the user, some code might execute differently.
Because this runAs method will create opportunities as different users and I have a trigger on Opportunity which executes a SOQL on the User object, the SOQL itself is executed as the user defined in the runAs method. Now for some unclear reason, I receive an error that there was no record to assign to a variable (see example)
User aUser = [ SELECT Id FROM User WHERE isActive = true AND Profile.Name = 'System Administrator' LIMIT 1 ];
Every user / profile associated with user which could possibly run this query has at least read access to the user object (sharing rules). I don't know if they have default access to the Profile object, but if that would be the problem, I'd expect another error message. The one I receive is the following:
System.QueryException: List has no rows for assignment to SObject
I'd expect an insufficient cross-object access error if Profile.Name would not be available...
Do you have insights on that?
Please use like this it will not throw you MIX_DML_Operation Error .
All Answers
public static User createUser(String profileName){
Profile profile = [select id from Profile where Name =:profileName limit 1];
string uName;
if(profileName.equalsIgnorecase(PP_Constants.WAREHOUSE_MANAGER_PROFILE_NAME)){
uName = 'whmgr@testorg.com';
}else if(profileName.equalsIgnorecase(PP_Constants.SALES_REP_PROFILE_NAME)){
uName = 'salesrep@testorg.com';
}
User userObj = new User(Alias = 'dev501', Email='dev501cert@testorg.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = profile.Id,
TimeZoneSidKey='America/Los_Angeles', UserName=uName);
return userObj;
}
I think below link have some code it will solve your problem .Instead of query from User you can query from profile and create usser record and use that in runAs ().
Please check and let me know any issue .
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_tools_runas.htm
Unless your suggestions work without actually having to insert the user...
I'm referring to this article:
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dml_non_mix_sobjects.htm
I don't know the exact phrasing of the error message.
Please use like this it will not throw you MIX_DML_Operation Error .
I think I'm going to create a static god user with admin rights and then let him run this piece of code every time.
Username = Datetime.now().getTime() + 'x@y.com'
Yeah would be a got to but it seems my static user works just fine. I use some sort of a constructor class anyway for my most used objects, ehich includes users. I just expanded it by a static user variable for the admin and a static function to create it...
Thanks