Salesforce Certified Platform Developer Practice Questions

Salesforce Certified Platform Developer exam preparation

Salesforce Certified Platform Developer practice questions in English: 180 questions with explanations. The mock exam has 60 questions and 105 minutes.

Salesforce Certified Platform Developer free question 7 / 10 · Domain 3: User Interface

A developer wants the query in the following class to respect the user's sharing rules. A user action calls new OrderService.Finder().findAll(). public with sharing class OrderService { public class Finder { public List<Order> findAll() { return [SELECT Id FROM Order]; } } } Which statement about the sharing declaration for the inner class Finder matches the Apex Developer Guide?

  1. The inner class does not inherit the sharing setting of its outer class, so declare Finder with sharing as well
  2. The inner class inherits with sharing from the outer class, so sharing rules apply without adding a declaration to Finder
  3. An inner class cannot have a sharing declaration, so add WITH USER_MODE to the query in Finder to enforce sharing rules
  4. Finder is part of OrderService, so stop calling it from outside and call it from a method added to OrderService instead
Show the answer and explanation

Answer: A

Why this is correct
The Implementation Details section of "Use the with sharing, without sharing, and inherited sharing Keywords" says that both inner classes and outer classes can be declared as with sharing, and that inner classes do not inherit the sharing setting from their container class. Otherwise the sharing setting applies to all code in the class, including initialization code, constructors, and methods. The with sharing on OrderService therefore does not reach Finder, so Finder needs its own with sharing declaration.

Why the other options are wrong
"The inner class inherits with sharing from the outer class, so sharing rules apply without adding a declaration to Finder" … The guide says the opposite: inner classes do not inherit the sharing setting from their container class. Inheritance of a sharing setting happens when one class extends another.
"An inner class cannot have a sharing declaration, so add WITH USER_MODE to the query in Finder to enforce sharing rules" … The guide says inner classes can be declared as with sharing, so Finder can carry its own declaration and nothing forces a switch to user mode. The type that cannot have an explicit sharing declaration is an Apex trigger.
"Finder is part of OrderService, so stop calling it from outside and call it from a method added to OrderService instead" … Calling Finder from a method of OrderService changes nothing. The sharing setting of the class where a method is defined is applied, and Finder's methods are defined in Finder.

Related rules
The same section says a class that extends another class inherits the sharing setting of its parent, and that Apex triggers cannot have an explicit sharing declaration. The guide also states that Apex without an explicit sharing declaration is insecure by default, so every class should declare one.

Go to the Salesforce Certified Platform Developer practice app (10 free questions; purchase unlocks all questions)

See the full practice set