Skip to content

Instantly share code, notes, and snippets.

@msrivastav13
Last active March 17, 2025 13:53
Show Gist options
  • Save msrivastav13/7b802b44f51b4a4d8d7235fd0ec3902d to your computer and use it in GitHub Desktop.
Save msrivastav13/7b802b44f51b4a4d8d7235fd0ec3902d to your computer and use it in GitHub Desktop.
Sample code that shows how to invoke an Agentforce Agent from Apex
public with sharing class AgentAIInvoker {
public static void invokeAIAgent() {
try {
// Create an instance of the invocable action with type 'generateAiAgentResponse' and name 'Agentforce_Service_Agent_new'
Invocable.Action action = Invocable.Action.createCustomAction(
'generateAiAgentResponse',
'Agentforce_Service_Agent_new'
);
action.setInvocationParameter('userMessage', 'Summarize my case');
action.setInvocationParameter('CaseId', '500VW0000022nZlYAI');
// 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(
'Apex action execution failed: ' + result.getErrors()
);
}
} catch (Exception e) {
System.debug('Error invoking Apex action: ' + e.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment