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
Saravanan @CreationSaravanan @Creation 

System.runAs() method on APEX Class.-Urgent

Hi Team,

 

I have one doubt on the System.runAs() method .

 

Can I use this system.runAs() in the Apex class not the test class?

 

I want to use this method in apex class to execute block of code based on the system adminstrator user.

 

Please help me .If i can use then what are the procedure i need to take care.If not please give me the reson.

 

Thanks in advance

Devendra@SFDCDevendra@SFDC

Hi,

 

Note: You can only use runAs in test method.

 

If you want to execute a code for System Admin user then you can do something like below:

 

Profile objProfile = [Select Id, Name from Profile where Name =: 'System Administrator' limit 1];

User objeUser = [Select Id, ProfileId from User where Id=: UserInfo.getUserId()];

// Compare the logged in user has System Administrator profile
if(objUser.ProfileId == objProfile.Id){
// Execute code
}

 Hope this helps :)

 

Saravanan @CreationSaravanan @Creation

Hi ,

 

Thanks for your quick reply,

 

So you are telling that we can't use system.runAs method in apex class.Am I right?

 

 

 

Bhawani SharmaBhawani Sharma
Nope, Devendra is saying you can not use System.runAs in test class. But if want to change the context of execution in Apex class, you can simply change the user profile as mentioned by Devendra above.
Devendra@SFDCDevendra@SFDC

 

I am modifying my above comment as, You can use System.runAs() in apex class but only when it comes under test method.

 

Please follow below example:

 

http://developer.force.com/cookbook/recipe/using-system-runas-in-test-methods

 

If you want execute some code in the context of System Admin you can try the apex logic as I have explained above.

 

 

Bhawani SharmaBhawani Sharma
Devendra is correct :) :)
Saravanan @CreationSaravanan @Creation

Ok thats fine,

 

So i will not use this method in apex class.

 

I have one class with sharing  keyword in that i want to query the permissionset assigment.

 

Logged in user don't have modify all permission. so it will through insufficient privilages. As per the below article .

 

http://help.salesforce.com/apex/HTViewSolution?id=000163404&language=en_US

 

please help me how can i resolve this .

 

because of this i try to use RunAs in the class to query the permission set assigment.

 

 

Sean TanSean Tan

So from what I'm understanding, you want to bypass sharing for that individual query right? You can try something like this:

 

public with sharing class MyClass
{
	public static void doStuff()
	{		
		Records[] list = new MyClassUtil().executeQuery();		
		//Do stuff here
	}
	
	/**
	 * Class to bypass sharing for the single case where non-admins need to query the permission set
	 */
	private without sharing class MyClassUtil
	{
		public Records[] executeQuery()
		{
			//execute and return the records here
		}
	}
}

 The inner class will be the only thing that runs outside of sharing context, everything else will still be in sharing context.

Saravanan @CreationSaravanan @Creation

Thanks sean ,

 

Its not working still i am getting the same problem.

Jerun JoseJerun Jose
Could you post your current code. I believe Sean's code using the 'without sharing' should be able to query the permission set assignment object.
Timothy SinardTimothy Sinard
Devendra@SFDC is correct in that you cannot use runAs outside of a test class. However, Devendra@SFDC proposed a solution that will not solve your problem. Their solution will only execute your code if the user is System Administrator, it will not change the execution context. This means if a Standard User tries executing the code then it will not run.
 
Himanshu Rana 7Himanshu Rana 7

Hey Find out this post to know more about System.runAs() Method

http://himanshurana.in/system-runas/

Blng QA3 AdminBlng QA3 Admin
Hi Saravanan, 

Did you find a solution for this ?