I need to execute a block of code in apex class with context/visibility of a specific user. This user is mentioned in a custom field of a custom object. The need is similar to System.Runas() method however this method can be used only in test class not in apex class. Without using 'with sharing' apex class will run in system admin context and using 'with sharing' it will run with the context of user who invoked the transaction. However in my case it should be any of these two but a specific user. Is there any solution or workaround for this?

  • Can that part of your code be run asynchronously? You have your question tagged with visualforce and mention a "class". Is this question about a "controller" for a visualforce page or perhaps a support class that it calls? If so, being more specific and providing sample code would be very helpful in getting an answer you'll find useful. – crmprogdev Dec 17 '16 at 20:00
  • You need to be clearer -- Apex runs in system context - always - and ignores running user's CRUD privileges unless you specifically test for them with the describe calls. with sharing and without sharing have to do with record visibility in the context of the running user. Inner classes can be used to enforce/relax this for a given operation. Please clarify by using edit to amend your question – cropredy Dec 17 '16 at 20:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.