+ Start a Discussion
Gopinath418Gopinath418 

@future method would work in system mode or user mode

I have a apex class with 'Without Sharing' annotation and i am calling a future method. The future method is not allowing some users (Non Admin)  to delete objectTerritory2Association records. showing insufficient access- read only access. Since i am running the class with 'Without Sharing' it must not throw an error right? but is throwing. 

Thanks,
Gopinath
Gopinath418Gopinath418
Can anyone help on this?

Regards,
Gopinath
Krishna.AvvaKrishna.Avva
Hi Gopinath,

Is the call from an @Auraenabled class? Please find the combinations according to the latest release: 

: https://releasenotes.docs.salesforce.com/en-us/winter20/release-notes/rn_lc_apex_with_sharing_cruc.htm (https://releasenotes.docs.salesforce.com/en-us/winter20/release-notes/rn_lc_apex_with_sharing_cruc.htm" style="color:blue; text-decoration:underline)

 
    • Child class is called from an @Auraenabled method from a “public class without sharing” – Child class will run in without sharing mode (System Mode)
    • Child class is called from an @Auraenabled method from a “Public Class with sharing” – Child class will run in with sharing mode (User Mode).
    • Child class is called from an @Auraenabled method from a “Public class” – Child class will run in with sharing mode (User Mode).
Gopinath418Gopinath418
Hi Krishna,

Thanks for your reply.

My scenario is:
i have a class (not defined with sharing or Without sharing ). From this class i am calling another class (which is defined with without sharing keyword) method.

As per your comment it should work in user mode (Child class is called from an @Auraenabled method from a “Public class” – Child class will run in with sharing mode (User Mode).)

But i have seen below statements regarding with sharing and without sharing, in which it is said The sharing setting of the class where the method is defined is applied, not of the class where the method is called.

Implementation Details About with sharing and without sharing Keywords
  1. The sharing setting of the class where the method is defined is applied, not of the class where the method is called. For example, if a method is defined in a class declared with with sharing is called by a class declared with without sharing, the method executes with sharing rules enforced.
  2. If a class isn’t declared as either with or without sharing, the current sharing rules remain in effect. Therefore, the class doesn’t enforce sharing rules except when it acquires sharing rules from another class. For example, if the class is called by another class that has sharing enforced, then sharing is enforced for the called class.
  3. Both inner classes and outer classes can be declared as with sharing. The sharing setting applies to all code contained in the class, including initialization code, constructors, and methods.
  4. Inner classes do not inherit the sharing setting from their container class.
  5. Classes inherit this setting from a parent class when one class extends or implements another.
As per second point my method should acquire security level from the class where it is defined i.e Without Sharing.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_keywords_sharing.htm

Could you please help to understand it ?

Thanks,
Gopinath
Krishna.AvvaKrishna.Avva

Hi Gopinath,

The scenarios which i gave are only for @Auraenabled classes with the specific update activated in your org. 
Since you are class is not @Auraenabled the sharing rules which you posted from the help should hold good. 

"For example, if the class is called by another class that has sharing enforced, then sharing is enforced for the called class."Considering this point and since your outer class is not declared with or without- which will default the sharing to "With Sharing" i think you are running into the issues. 

The best way to confirm this is - giver delete permission to a profile and try to delete it.. If it works you might need to reorganize your sharing and wit sharing keywords for best user experience. 

Regards,
Krishna Avva