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?