Advanced 30 min read

Secure Data Access

By the end of this lesson, you'll be able to:

  • Apply Module 28's sharing model to the trigger handler and any supporting service class
  • Decide whether this feature needs with sharing or without sharing
  • Explain the reasoning, not just the choice

Prerequisites: "Automate Case Routing"

The question to ask

Module 28 taught that with sharing is generally the safer default, reserving without sharing for genuinely deliberate reasons. Applying that here: does Case auto-routing need to see Cases the running user couldn't normally see? No — a trigger only ever operates on the Cases actually being inserted in this transaction, which the creating user already has access to by definition.

Declaring the handler with sharing

public with sharing class CaseTriggerHandler {
    public void beforeInsert(List<Case> newCases) {
        // ... routing logic from previous lessons ...
    }

    private Map<String, String> buildRoutingLookup() {
        // this query reads Case_Routing_Rule__mdt — custom metadata,
        // which isn't subject to record-level sharing the way a
        // custom object would be
        return new Map<String, String>();
    }
}

with sharing on CaseTriggerHandler is the right, deliberate choice here — nothing in this feature has a genuine reason to bypass the running user's normal record access.

Why this reasoning matters more than the answer itself

The specific conclusion here ("use with sharing") is less important than the process Module 28 taught: ask explicitly whether a genuine reason exists to bypass sharing, rather than defaulting to without sharing out of habit or copying an existing class. For this feature, that explicit question has a clear, defensible answer — which is exactly what a code reviewer would want to see justified, not just asserted.

Exercise

As a comment, write the reasoning (not just the answer) for whether CaseTriggerHandler should use with sharing or without sharing.

Show hint

Does this feature need to see records the running user couldn't normally see?

APEX

Secure Data Access Quiz

1. What question should be asked before choosing without sharing for a class?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

This lesson revisits Module 28's sharing lesson specifically for this feature — deciding, with real reasoning, whether the routing logic should run with or without sharing.