1

I have a controller ControllerClassA which is with sharing. And i also have another class HelperClassB which is without sharing. I have a method inside a without sharing class HelperClassB which is called methodA.

I want to use this method inside with sharing class ControllerClassA with sharing applied.

please let me know if it is possible.

1 Answer 1

5

If you want a method that can be used from two different contexts with or without sharing applied, the right approach is to put that method in a separate class that is declared as "inherited sharing", then simply call that method from the classes that are with or without sharing as required. The method will then apply or not sharing rules.

public inherited sharing class WithOrWithoutSharing {
    public static void myMethod() {
        ...
    }
}

public with sharing class WithSharing {
    public void somethingWithSharing() {
        // Will apply sharing
        WithOrWithoutSharing.myMethod();
    }
}

public without sharing class WithoutSharing {
    public void somethingWithoutSharing() {
        // Will NOT apply sharing
        WithOrWithoutSharing.myMethod();
    }
}
1
  • Thanks much.... i will apply the same
    – Sisir
    Commented Apr 25, 2019 at 7:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .