The independent daily for Salesforce admins, developers and architects

Curated by Abhinav (opens in a new tab)

Must-read

Running custom Apex actions in user mode for assistant-driven access (opens in a new tab)

You will learn why Apex invocable actions exposed to Copilot or agents should use with sharing, WITH USER_MODE and AccessLevel.USER_MODE, and how to test them under a low-privilege user with System.runAs.

  1. Declare the class with sharing
  2. Query with WITH USER_MODE
  3. Update with AccessLevel.USER_MODE
  4. Return readable messages to the assistant
How the example action keeps the running user's permissions in force

The author starts from the Copilotforce announcement, in which Salesforce data is reached from Microsoft Copilot through authenticated user access that keeps existing permissions, Sharing Rules and audit trails. Standard writeback actions already respect these controls, but any custom invocable Apex, Flow calling Apex or REST endpoint is the developer’s responsibility.

Apex runs in system mode by default, so an invocable method that queries a field the user cannot see will return that data to an assistant acting on their behalf. A class declared without with sharing also ignores the user’s record access. When people worked mainly in the Salesforce UI, page layouts hid much of this; with Copilot, Claude or Slack as the interface, the server-side enforcement is what protects the data.

The article gives an example invocable class, ApprovePaymentAction, declared with sharing. Its SOQL uses WITH USER_MODE so field and object permissions apply, and its update uses Database.update with AccessLevel.USER_MODE and allOrNone set to false, so Validation Rule messages come back as strings the assistant can show. The author suggests writing those messages so they read well in a chat window.

The post closes with a runAs test that runs the action as a Standard User and asserts the approval is blocked, and a checklist: with sharing, WITH USER_MODE on every query, AccessLevel.USER_MODE on every DML, readable validation messages, field history tracking on changed fields, and a low-privilege runAs test for each exposed action.

Read the original at dev.to (opens in a new tab)

Also told by InfallibleTechie (opens in a new tab)

Worth reading?

A small owned trigger framework built around an explicit handler contract (opens in a new tab)

You will learn how to structure an Apex trigger framework with a single dispatcher, a recursion guard that tracks record IDs, layered bypass controls via Custom Metadata and a permission, and user-mode selectors with a savepoint-based unit of work.

Why read this

The author also explains why they chose an owned baseline over a third-party framework.

  1. Check bypass layers
  2. Confirm trigger context
  3. Check recursion guard
  4. Mark records as handled
  5. Call context hook
The trigger lifecycle checks run in a fixed order before the context hook is called

The author argues that Salesforce trigger frameworks usually fail on their contract rather than their code: when a handler runs, when it is skipped, what happens on re-entry, and who may bypass it. Their baseline is deliberately small, with a one-line trigger that delegates to a dispatcher. The lifecycle checks bypass settings first, confirms the code is running inside a trigger, resolves the context, verifies the SObject type, checks the recursion guard, marks records as handled, and then calls the context hook. Handlers only override the contexts they need and delegate rules to domain classes.

The recursion guard tracks which record IDs a named handler has already processed in the transaction, instead of using a static boolean. This avoids both blocking legitimate second passes for different records and re-entry from after-triggers. Bypass works in three layers: a runtime bypass for tests and controlled operations, an Is_Active__c flag on Custom Metadata to disable a handler without deployment, and a Custom Permission for integration users during data loads. Metadata is read once per transaction. The surrounding layers keep business logic out of the trigger, with selectors running in user mode and a unit of work that commits DML in a fixed order inside a savepoint.

The author reports that static checks and a check-only compile against an org caught configuration issues, and that the test suite exposed a null dereference outside trigger context and a wrong assumption about scheduled job execution in tests. The article closes by listing next steps, including removing defaults from test factories and enforcing the Permission Set in the deploy pipeline.

Read the original at dev.to (opens in a new tab)

Worth reading?

Recommended

Securing Salesforce to LLM integrations with Apex and scoped permissions (opens in a new tab)

Readers will learn how to enforce least-privilege access when calling external language models from Apex, including proper use of WITH USER_MODE, Named Credentials, and response validation.

The article argues that connecting Salesforce to a large language model takes more than an API key. The integration should control which records and fields the model can reach, protect credentials, minimize the data sent, and stop AI output from triggering unauthorized actions. It proposes an architecture with five parts: Salesforce enforcing permissions, Apex or middleware handling authorization and request building, Named Credentials managing the endpoint and authentication, the LLM API generating output, and a validation layer checking results before they are shown or used. A support assistant summarizing Cases, for example, needs only subject, description and status, not full contact or financial history. Authorization should happen before data is sent to the model and again before any business action follows its response.

The piece distinguishes several permission mechanisms that must be combined rather than relying on OAuth scopes alone: integration identity, object permissions, field-level security, record sharing and application-level restrictions. In Apex, declaring a class with sharing does not enforce object and field permissions, so the article recommends WITH USER_MODE on supported SOQL queries, with Security.stripInaccessible() as an alternative for removing inaccessible fields. For credentials, it recommends never hardcoding API keys and instead using External and Named Credentials, granting access through Permission Sets, and keeping separate configurations for each environment. A sample Apex class summarizes a Case through the OpenAI Responses API, querying only needed fields, sending a minimal payload, and checking the HTTP status and response structure before returning text.

The article also covers defending against prompt injection, since record text from emails or tickets may contain malicious instructions. Its safeguards include treating retrieved text as untrusted, limiting fields and records, avoiding unrestricted query or tool access for the model, checking proposed operations against a server-side allowlist, requiring human approval for consequential changes, and filtering documents by user access before they reach the model in retrieval-based designs. For scale, it suggests asynchronous processing with Queueable Apex or middleware for longer tasks, bounded retries with backoff only for suitable errors, request identifiers to prevent duplicate actions, token and latency monitoring, and cache designs that respect permission changes. The conclusion urges starting with a read-only use case and adding write capabilities only after authorization and failure handling are tested.

Read the original at dev.to (opens in a new tab)

Worth reading?