Function Call

Function Calling enables the AI to act as an Agent, chaining multiple actions to achieve a complex goal. Instead of simply answering a question, the AI can search for data, update records, and create new entries in a single conversation flow.

How it works (The Loop)

  1. User Request: You ask the AI to perform a multi-step task (e.g., “Find the last open case and close it”).
  2. Tool Selection: The AI analyzes the request and decides it needs to run a specific tool (e.g., a search query).
  3. Execution: Salesforce executes the requested function (Apex) and returns the raw data (e.g., record IDs, status) back to the AI.
  4. Reasoning: The AI analyzes the returned data. It determines the next necessary step (e.g., “I found the case, now I need to update its status”).
  5. Action: The AI calls the next function (e.g., CRUD update).
  6. Response: Once all steps are complete, the AI confirms the actions to the user.

Example Scenario: Case Management Agent

This scenario demonstrates how the AI orchestrates multiple Salesforce actions (Query, Update, Create) to fulfill a complex user request.

User Prompt:

“Find the last unclosed case and close it. Also, change the Engineering Request Number to 000001 and add a case comment stating that the number was changed.”

AI Execution Steps:

  1. Schema Retrieval: The AI requests the schema for the Case object to understand field names (specifically looking for custom fields like EngineeringReqNumber__c).
  2. Search (Query): The AI generates a SOQL query to locate the target record: SELECT Id, CaseNumber, Status, … FROM Case WHERE IsClosed = false ORDER BY CreatedDate DESC LIMIT 1.
  3. Analysis: Salesforce returns the case details (e.g., Case Number: 00001024, Current Req Number: 868314).
  4. Update Record: The AI calls the CRUD function with type: update to:
    • Set Status to Closed.
    • Update EngineeringReqNumber__c to 000001.
  5. Create Record: The AI calls the CRUD function with type: create to insert a new CaseComment:
    • Body: “Engineering Request Number changed from ‘868314’ to ‘000001’.”
  6. Confirmation: After the system successfully executes both transactions, the AI reports back to the user: “Case 00001024 has been closed, Engineering Request Number updated, and comment added.”

 Token Consumption Warning

Using Function Calls significantly increases token usage compared to standard chat interactions.

  • Context Overhead: Every step of the loop (Schema definitions, Query results, Tool inputs/outputs) is added to the conversation history and sent back to the AI for the next step.
  • Cost Implication: A single user request might trigger 3-5 internal exchanges between Salesforce and the AI model. For example, the scenario above involves retrieving a record, updating it, and creating a child record, passing data payloads multiple times. This counts against your API token quota.

Create Function Call

To create a Function Call, you need to:

1. Create a model based on Chat GPT.

2. Press “Function Calls”

Content Image

3. Click on “New Function Call”

Content Image

A window will open with the following fields to fill in:

  • Function Call Name – The title must start with a letter or an underscore. Must be alphameric (a-z, A-Z, 0-9), underscores (_), dots (.) or dashes (-), with a maximum length of 64.
  • Function Name (Required field) – The title must start with a letter or an underscore. Must be alphameric (a-z, A-Z, 0-9), underscores (_), dots (.) or dashes (-), with a maximum length of 64.
  • Description
  • Function Implementation Path – Name of your Apex Class

Content Image

After Function Call creation , we will create parameters.

  • Click on “Add Parameters”

Content Image

The following window will open.

Content Image

The following types are available when creating parameters:

  • String

Content Image

The following fields are available for filling in:

  • Required – Checkbox that makes this parameter required 
  • Parameter Name (Required Field)
  • Description
  • Min – Minimum string length.
  • Max – Max string length.
  • Pattern – Regex pattern. Example ^[0-9]{5}$
  • Format
    • email
    • uri
    • uuid
    • date-time
    • date
    • time
    • hostname
    • ipv4
    • ipv6
  • Enum – Available list in JSON format
  • Integer

Content Image

The following fields are available for filling in:

  • Required – A checkbox that makes this parameter required 
  • Parameter Name (Required Field)
  • Description
  • Min – Minimum string length.
  • Max – Max string length.
  • Number

Content Image

The following fields are available for filling in:

  • Required – A checkbox that makes this parameter required 
  • Parameter Name (Required Field)
  • Description
  • Min – Minimum string length.
  • Max – Max string length.
  • Pattern – Regex pattern. Example ^[0-9]{5}$

Content Image

The following fields are available for filling in:

  • Parameter Name (Required Field)
  • Description
  • Object (Reserved for future use)
  • Array (Reserved for future use)

Once you have created the parameters according to your prompt, the AI will generate a response for you, and your Apex code will parse it into the correct fields for quick date handling.

Content Image

To use our Function Call, you need to create an Apex class:

Click File -> New -> Apex Class

Content Image

Let’s take the example of searching for a user and creating a task for that user.

In the first case, we have one parameter, the name we are searching for. Our Apex Class, which implements “peeklogic_ai.Function_call,” parses our response, and we obtain our name.

Content Image

In the second case, we have several parameters. Accordingly, our apex class already structures them for quick work with them. And we have direct access to the date that we have recorded in the parameters.

Content Image

Prompt: “Create a high-priority ticket for user 0058Z00000BvNke with subject “Urgent issue in checkout” and description “Customer reports that the checkout page is freezing.” Set the status to “Not Started” and the due date to tomorrow. Today 22.07.2025

Response:

Content Image

Thanks to these structured parameters, we can quickly create our Task.

Content Image

 

Ready to simplify your Salesforce integrations?

    Peeklogic AI Orchestrator