You need to sign in to do that
Don't have an account?

Run apex code under another user
Is it possible to run Apex code under a different user than the logged in one?
So someone has logged in and they kick off some Apex code, but I need that specific method to run under an admin user? Is that possible?
Thanks.
So someone has logged in and they kick off some Apex code, but I need that specific method to run under an admin user? Is that possible?
Thanks.
In test class create user with test profile
user objUser = new user(name='', profileid='', etc);
insert objUser;
Then use
system.RunAs(objUser)
{
//test logic here
}
Please mark this answer as best if it helped you.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_keywords_sharing.htm
Hope that helps!
It will always run as the logged in user or system mode. If you have specific user with which you want to run the code then there's 1 way i can think of but that will be the last one you would want to follow and also that will need the credentials of the user with which you want to run the code.
Login through that user using rest api and run the method. http://www.cloudforce4u.com/2015/10/rest-api-integration-salesforce-apex.html
You can use runAs only in test methods.
I have not found any exception to this rule, nor any workaround.