本文へ移動
cccskills
無料GitHub で公開

agent-action-error-handling

Design Invocable Apex actions that return deterministic, agent-friendly errors instead of surfacing raw exceptions to the LLM. NOT for authoring the action itself — @InvocableMethod schema, labels, security context — use agentforce/custom-agent-actions-apex. NOT for the Apex tests that force each error branch — use agentforce/agent-action-unit-tests.

インストール方法を見る

含まれるファイル(7)

  • SKILL.md7.4 KB
  • references/examples.md18.9 KB
  • references/gotchas.md12.9 KB
  • references/llm-anti-patterns.md10.6 KB
  • references/well-architected.md8.8 KB
  • scripts/check_agent_action_error_handling.py946 B
  • templates/agent-action-error-handling-template.md556 B

SKILL.md(原文)

インストールする前に、エージェントに与えられる指示の中身を確認できます。

Agent Action Error Handling

Agentforce agents receive the return value of an Invocable action as-is and feed it back into the LLM loop. An unhandled exception becomes a Flow or framework error — the agent cannot reason about it, often re-invokes the action with the same inputs, and occasionally confabulates success. This skill defines a Response type that always returns (status, reason_code, user_message) so the subagent instructions can route deterministically.

Terminology. Salesforce renamed agent topics to subagents in April 2026. Nothing about behaviour changed, and the API surface kept the old word — GenAiPlugin is still the metadata type behind a subagent, and GenAiFunction is still the action. This skill says subagent instructions for what older docs and your org's metadata call topic instructions.

The Platform Rule Everything Follows From

An @InvocableMethod must return one result per input, in the same order, even when errors occur — the inputs and outputs "must match on both the size and the order" (InvocableMethod Annotation).

So an error is not an exception you throw. It is a value you return in the slot belonging to the failing request. A thrown exception breaks the contract for the whole batch, not just the request that failed.

Two further platform rules shape the class: the method must be static, public or global, and live in an outer class; and there is one invocable method per class. One action is one verb — see templates/agentforce/AgentActionSkeleton.cls.

The Envelope

FieldTypePurpose
statusOK | USER_ERROR | SYSTEM_ERRORLocus of control — whose problem is this
reasonCodestable enum stringWhat the subagent instructions branch on
userMessage≤140 chars, authoredSafe to read aloud verbatim
retryableBooleanWould the same inputs plausibly succeed

status and retryable are independent axes. A row lock is a retryable system error; a validation failure is a terminal user error; a 503 is a retryable system error. Collapsing them into one Boolean success is the single most common cause of agent retry loops.

Recommended Workflow

  1. Design the Response class with the four fields above. Register every reasonCode in Agent_Reason_Code__mdt so Apex, subagent instructions, and dashboards share one contract.
  2. Structure the method in two phases — validate and collect, then one bulkified Database.<dml>(records, false, AccessLevel.USER_MODE) — and assign results back by index into a pre-sized output list. Per-row DML raises an uncatchable LimitException under bulk invocation, which defeats every catch you write.
  3. Classify by locus of control, branching on Database.Error.getStatusCode() or the HTTP status code — never on message text, which is localised and reworded between releases. Log the raw exception to templates/apex/ApplicationLogger.cls; never put getMessage() in userMessage.
  4. Write one test per reason code asserting the code, the retryable flag, and that no raw platform error text reached userMessage. Coverage percentage is not the signal; assertions-per-reason-code is.
  5. Add one subagent instruction per outcome class, including an explicit "do not call the action again" for terminal codes — the planner's default on an unsatisfied goal is to retry.
  6. Run the Testing Center / sf agent test run scenarios that exercise each branch and confirm the agent's wording matches the instruction for each code.

Key Considerations

  • userMessage is user-visible and lands in a stored transcript. Validation-rule error text can interpolate field values, so getMessage() is a PII channel as well as an implementation-detail leak.
  • Distinguishing NOT_FOUND from NO_ACCESS helps internal users and leaks record existence on a guest channel. Decide per channel.
  • retryable = true on a mutating action requires idempotency (a caller-supplied key upserted on an External Id). Otherwise a retried timeout is a duplicate write.
  • The @InvocableMethod and @InvocableVariable description values are read by the planner to decide whether to call the action, and pre-fill the instruction fields in Agentforce Builder — they are functional, not documentation.
  • Watch the SYSTEM_ERROR/UNKNOWN ratio in production. It is the direct measure of classification debt; a jump after a release means an upstream contract changed.

Worked Examples (see references/examples.md)

  • Typed Response envelope for a Case-update action — Agentforce service agent closes Cases on user request.
  • Stable reason_code enum across releases — A new DML error type appears after a managed-package install.

Common Gotchas (see references/gotchas.md)

  • Throwing AuraHandledException from an Invocable — The agent receives an opaque framework message and loops.
  • Governor-limit exceptions pre-empt your catch — LimitException (uncatchable) kills the transaction — agent sees raw Flow failure.
  • Empty list returned when input list is empty — Agent receives an empty array and hallucinates a success message.

Top LLM Anti-Patterns (full list in references/llm-anti-patterns.md)

  • Rethrowing exceptions from an @InvocableMethod — the LLM cannot reason about framework errors.
  • Putting ex.getMessage() directly into user_message — leaks internals and breaks deterministic subagent routing.
  • Using boolean success instead of a reason_code enum — the agent cannot distinguish retryable from terminal failures.

Official Sources Used

レビュー

まだレビューはありません。使ってみた感想をお寄せください。

同じリポジトリのスキル

概要と使いどころ

Use this skill when writing test-first, behavior-driven acceptance criteria in Given/When/Then format for a Salesforce user story. Covers happy path, edge cases, negative paths, permission boundaries, and data-state preconditions so the AC block can drive UAT scripts and Apex test design downstream. Trigger keywords: given when then, gherkin, behavior driven AC, test first acceptance criteria, scenario outline, BDD acceptance criteria. NOT for the user-story format itself (use admin/user-story-writing-for-salesforce). NOT for UAT script writing (use admin/uat-test-case-design). NOT for Apex test method generation (use agents/test-class-generator). NOT for high-level UAT planning (use admin/uat-and-acceptance-criteria). More triggers: acceptance criteria record, ac_id, req_id on a criterion, artefact under test, negative case for a rule-type requirement, no-match fall-through criterion, oracle for a Then clause, test type apex flow manual, criteria linter, check_ac_format.py, --manifest-dir.

日本語の概要は準備中です。原文の説明を表示しています。

PranavNagrecha/AwesomeSalesforceSkills192026年10月4日 更新

Task and Event objects: polymorphic WhatId/WhoId, Activity object model, ActivityHistory vs OpenActivity, activity timeline customization, bulk task creation, Einstein Activity Capture boundaries. NOT for turning on EAC or calendar sync — use admin/einstein-activity-capture-setup. NOT for Email-to-Case — use admin/case-management-setup. Covers ActivitiesSettings metadata, enableActivities, shared Task/Event custom fields and FLS, TaskStatus value sets, Shared Activities and TaskRelation/EventRelation, recurring and child activities, and partial-success bulk DML on Task.

日本語の概要は準備中です。原文の説明を表示しています。

PranavNagrecha/AwesomeSalesforceSkills192026年10月4日 更新

Use when designing how an Agentforce agent extracts structured input parameters (slots) from a user's natural-language utterance to invoke an action. Triggers: 'agent extract date from user', 'agent action argument extraction', 'agentforce slot filling', 'agent invocable input mapping', 'agent fails to fill required parameter'. NOT for action authoring (use agentforce/agent-actions) or for prompt-template variable binding (use agentforce/prompt-builder-templates).

日本語の概要は準備中です。原文の説明を表示しています。

PranavNagrecha/AwesomeSalesforceSkills192026年10月4日 更新

Apex test patterns for @InvocableMethod agent actions: per-reason-code branch coverage, bulk safety, callout mocking, deterministic assertions. NOT for testing whether the agent routes to the right topic or answers well — use agentforce/agent-testing-and-evaluation. NOT for designing the error envelope the tests assert on — use agentforce/agent-action-error-handling.

日本語の概要は準備中です。原文の説明を表示しています。

PranavNagrecha/AwesomeSalesforceSkills192026年10月4日 更新

Designing or reviewing Agentforce actions: Flow actions, Apex invocable actions, prompt-template actions, action naming, input and output contracts, confirmation requirements, and safe error behavior. Triggers: 'agent actions', 'build an agent that can look up an order', 'make the agent able to check order status', 'give the agent a capability', 'agent that can retrieve a record', 'flow action for agent', 'agent invocable action', 'action schema design'. NOT for writing the Apex class itself — use agentforce/custom-agent-actions-apex. NOT for the reason_code error envelope that stops an agent retry loop — use agentforce/agent-action-error-handling.

日本語の概要は準備中です。原文の説明を表示しています。

PranavNagrecha/AwesomeSalesforceSkills192026年10月4日 更新

Use when deploying an Agentforce agent to a channel surface: Embedded Service web chat, Slack, Experience Cloud, Agent REST API, or Salesforce Mobile. Triggers: 'deploy agent to website', 'add Agentforce to Slack', 'enable agent for mobile app', 'configure embedded service for agent', 'agent API for custom integration', 'multi-channel agent deployment'. NOT for agent logic design — use agentforce/agent-topic-design or agentforce/agent-actions for topics, instructions, and action contracts.

日本語の概要は準備中です。原文の説明を表示しています。

PranavNagrecha/AwesomeSalesforceSkills192026年10月4日 更新

PranavNagrecha のスキルをすべて見る

このスキルの問題を報告する