Interview Example Answer:
“System Mode runs with full privileges and ignores user sharing, while User Mode respects the current user’s permissions and FLS.”
👉 “System Mode 拥有完全访问权限并忽略共享规则,User Mode 遵守用户权限与字段级安全。”
| Mode / 模式 | Description (EN / 中文说明) |
|---|---|
| System Mode | Runs Apex code ignoring user’s object, field, and record-level permissions.👉 忽略用户对象、字段与共享权限,以系统身份执行。 |
| User Mode | Executes code respecting user’s profile, field-level security (FLS), and sharing rules.👉 遵守用户的 Profile、字段安全与共享规则。 |
| Scenario / 场景 | Mode / 模式 | Description / 中文说明 |
|---|---|---|
Without runAs() |
System Mode | 默认系统模式,忽略权限限制。 |
With System.runAs() |
User Mode | 模拟特定用户权限与共享规则。 |
Interview Example Answer:
“By default, test classes run in System Mode.
To simulate real user access, we wrap code in
System.runAs()to enforce user mode.”👉 “测试类默认以系统模式运行;若需模拟真实用户访问,可用
System.runAs()切换到用户模式。”
| Component / 组件 | Mode / 模式 | Notes / 说明 |
|---|---|---|
| Apex Class | System Mode | 默认系统模式;若声明 with sharing 则启用共享规则。 |
| Trigger | System Mode | 若调用类使用 with sharing,则遵循共享规则。 |
| Anonymous Apex | User Mode | 在当前用户上下文执行。 |
| Workflow / Validation / Process Builder | System Mode | 忽略用户权限,系统全权执行。 |
| Flow | Default: User Mode | 被 Process Builder / Workflow 调用时 → System Mode。 |
| Batch / Queueable / Schedule Apex | System Mode | 异步任务统一以系统权限运行。 |
| Visualforce Page (Standard Controller) | User Mode | 遵循当前用户权限。 |
| Visualforce Page (Custom Controller) | Depends | 取决于 Controller 是否声明 sharing。 |
| Test Class | System Mode | 使用 System.runAs() 时变为 User Mode。 |