自訂工作人員可叫用動作:Apex 類別範例
從 Apex 類別呼叫自訂工作人員可叫用動作,以透過 Agentforce 服務代理程式或預設 Agentforce 代理程式自動完成工作。Apex 類別必須傳回工作人員的回應。
必要版本
| 提供版本:Lightning Experience |
| 提供版本:Enterprise、Performance、Unlimited 及 Developer Edition。所需附加元件授權會因工作人員類型而有所不同。 |
重要 從 2025 年 6 月 17 日開始,Agentforce (預設) 不會包含新功能或改善項目,且無法在新的 Salesforce 環境中使用。我們建議移轉至 Agentforce 員工工作人員以繼續增強和支援。如果您計畫轉換至 Agentforce 員工代理程式或進行相關授權變更,請提前完成現有 Agentforce (預設) 代理程式的移轉,以避免可能中斷工作人員可用性。如果您無法提前完成移轉,您可以在轉換後建立新的工作人員,這可能會涉及一些停機時間。請參閱 從 Agentforce (預設) 移轉至 Agentforce 員工代理程式。
| 需要的使用者權限 | |
|---|---|
| 若要建立與 Agentforce 相關聯的工作人員可叫用動作 (預設): | 「管理 AI 工作人員」和您工作人員類型的必要權限 或 自訂應用程式 |
| 若要建立與 Agentforce 服務代理人相關聯的工作人員可叫用動作: | 「管理 Agentforce 服務工作人員」和「管理 AI 工作人員」權限 或 自訂應用程式 |
自訂工作人員可叫用動作僅支援內容變數。若要建立內容變數,請在「傳訊工作階段」物件中建立自訂欄位,然後在 Agentforce Builder 中將其新增為變數。在這些範例中,caseId 和 leadId 是自訂欄位的 API 名稱。
此 Apex 類別會使用自訂工作人員可叫用動作的 1.1.0 版本呼叫預設 Agentforce 代理程式。它會使用 CaseEmail Apex 類別和個案識別碼傳回電子郵件。
public class AgentIAInvoker {
public static void invokeJavaAction() {
try {
// Create an instance of the invocable action with type ‘generateAiAgentResponse’, API name ‘Acme_Agent’, and version 1.1.0
Invocable.Action action = Invocable.Action.createCustomAction('generateAiAgentResponse', null, 'Acme_Agent', '1.1.0');
action.setInvocationParameter('userMessage', 'Generate an email with a summary of the case');
action.setInvocationParameter('caseId', '500VW0XXXXXXXXXXXX');
action.setInvocationParameter('apexClassName', 'CaseEmail');
// Execute the action
List results = action.invoke();
// Handle the response
System.debug('Result is: ' + results[0].getOutputParameters().get('structuredAgentResponse'));
}
}
}
此 Apex 類別使用自訂工作人員可叫用動作的 1.0.0 版呼叫 Agentforce 服務代理程式。它會根據商機識別碼傳回摘要。
public class AgentIAInvoker {
public static void invokeJavaAction() {
try {
// Create an instance of the invocable action with type ‘generateAiAgentResponse’ and name ‘Agentforce_Service_Agent_new’
Invocable.Action action = Invocable.Action.createCustomAction('generateAiAgentResponse', null, 'Agentforce_Service_Agent_new', '1.0.0');
action.setInvocationParameter('userMessage', 'Summarize my contact');
action.setInvocationParameter('leadId', '500VW0XXXXXXXXXXXX');
// Execute the action
List<Invocable.Action.Result> results = action.invoke();
Invocable.Action.Result result = results[0];
// Handle the response
if (result.isSuccess()) {
// Retrieve the Session ID and Agent Response
System.debug('Output Session ID: ' + result.getOutputParameters().get('sessionId'));
System.debug('Output Agent Response: ' + result.getOutputParameters().get('agentResponse'));
} else {
System.debug('Java action execution failed: ' + result.getErrors());
}
} catch (Exception e) {
System.debug('Error invoking Java action: ' + e.getMessage());
}
}
}
此文章是否解決您的問題?
請讓我們知道,以便我們改進!
