“Agent and Chill”: Enhancing Efficiency for Users Through the Use of Agents

5
(1)

The rise of Agentforce is here—the promise is to have intelligent agents that streamline workflows and enhance user interactions through a conversational interface, simplifying the user experience by just asking questions in plain language. But how difficult is it to build an agent? Let’s step into the shoes of a Salesforce admin responsible for handling this task.

Imagine our users are a marketing team relying on the Data Cloud segmentation feature. They have a few straightforward requests: as frequent segment users, they want to leverage the AgentForce interface to quickly understand segments details instead of navigating the traditional user interface. Specifically, they want to: check segment statuses, retrieve descriptions of what each segment does, and access a list of all segments in the system.

In this post, we’ll explore various methods for creating agents, ranging from no-code to pro-code, to better understand the available options.

Note: This is a proof-of-concept without error-handling or production-level testing and is not intended for production use. Always consult with your Salesforce architects or the equivalent approving authority before deploying any solution based on public examples.

An easy way to create an Agent action is to use a Flow.

Let’s create a basic agent that retrieves a segment’s publish status upon request. This involves setting up a Flow that first retrieves a Segment object, take the segment name as input, and return its publish status.

This ensures a seamless way to get segment details dynamically. Let’s dive into the implementation!

Create an action via a Flow

Let’s create a basic agent that retrieves a segment’s publish status upon request. This involves setting up a Flow that first retrieves a Segment object, take the segment name as input, and return its publish status.

This ensures a seamless way to get segment details dynamically. Let’s dive into the implementation!

Step 1: Create a new Flow

Create an auto-launched flow.

Step 2: Use a Get Record action

First, get a record from Data Cloud, in order to get the MarketSegment object name. Set the field Name to be equal to a new variable (as a new resource).

Step 3: Create a new resource

It is important to set the description field as this is used by the LLM! As as set this for input, so the user can input the segment name to retrieve the status from.

Step 4: Create an assignment action

Then create an assignment for the segment status: assign a new variable to a record field, the actual MarketSegment publish status.

This variable will be our output for the agent.

Then:

Step 5: Save the Flow

And now put description on the flow, and don’t forget to activate it.

Step 6: Create the agent topic

Create the agent topic (say, ‘Segment information’) and create our agent action:

And test the agent:

Create an action via Prompt Builder

This time, we want to support our marketing team, which manages a high volume of segments. With so many segments being created, it’s often unclear—especially when names or descriptions are missing—what each segment is doing. To address this, let’s develop an agent action that, when prompted, analyzes the segment filters and provides a clear explanation of what they do.
Another way to create an agent action is via Prompt builder; let’s use this in this case; you’ll see why in a moment.

Step 1: Use Prompt builder

Open up Prompt builder, choose Flex as the type and define the source as Segment.

Step 2: Retrieve Segment object

Then, from the Prompt builder page, find Segment Name, then Segment Include Criteria as resources to use in the prompt:

Step 3: Write the prompt

Write the prompt integrating these resources, such as the below, and test with an existing segment to see if you get the right result. When the use case involves summarisation, the simplest approach is to create a Prompt builder powered action that leverages LLM capabilities, rather than using alternative methods. Just using Include criteria in this example.
Example prompt:

Retrieve criterias of a segment object from its name {!$Input:SegmentCriteria.Name} , and
summarize its criteria, translating its JSON values of the include criteria {!$Input:SegmentCriteria.IncludeCriteria} and exclude criteria {!$Input:SegmentCriteria.ExcludeCriteria} in plain English. Denote which criteria are include vs. exclude via bullet points.

Step 4: Activate

Activate the prompt template; you can now update the segment agent, leveraging this Prompt template as a new action.

How about leveraging Apex?

If you need to implement something more complex—like most advanced tasks on the Salesforce platform—you can always use code instead of Flow. The same applies to Agentforce, where we can trigger an Apex action.

Step 1: Create Apex class

Let’s see this in action: suppose we want to create an agent that retrieves and lists all existing segments, including their statuses and counts, directly from the conversation panel.
Here’s the code below, along with a few key points:

  • The class returns a nested list, where each element is itself a List<MarketSegmentDTO>.
  • MarketSegmentDTO is a wrapper object that encapsulates market segment details.
  • Since there are typically multiple segments, we return a list of lists to organise the segment attributes properly.
  • The data is retrieved using an SOQL query
  • We make these parameters available as inputs by adding the InvocableVariable annotation above each one. This annotation exposes ‌class variables to actions as input or output parameters.

Note that a test class with proper test coverage is also mandated here for quality results.

public class DataCloudHealthSegmentDetails {        
    @InvocableMethod(label='Get Data Cloud Segment Details' 
                     description='Retrieves all MarketSegment records with key details.')
    public static List<List<MarketSegmentDTO>> getAllMarketSegments(List<Integer> numberRecords) {
 
    
        List<List<MarketSegmentDTO>> segmentList = new List<List<MarketSegmentDTO>>();
         List<MarketSegmentDTO> tmp = new List<MarketSegmentDTO>();


        try {
            // Set default limit if numberRecords is null or empty
            Integer limitValue = (numberRecords != null && !numberRecords.isEmpty() && numberRecords[0] != null) 
                                 ? numberRecords[0] : 10;

            // Safeguard against excessive query limits
            if (limitValue > 200) {
                limitValue = 200;
            }

            // Query MarketSegment records with dynamic limit
            List<MarketSegment> segments = Database.query(
                'SELECT Id, Name, SegmentStatus, PublishStatus, LastModifiedDate, LastSegmentMemberCount ' +
                'FROM MarketSegment LIMIT ' + limitValue
            );

            // Convert MarketSegment records to DTOs
            for (MarketSegment segment : segments) {
                 tmp.add(new MarketSegmentDTO(segment));
           }
           segmentList.add(tmp);
        } catch (Exception e) {
            System.debug('Error retrieving MarketSegment records: ' + e.getMessage());
        }
        return segmentList;
    }

    // Wrapper DTO Class
    public class MarketSegmentDTO {
        @InvocableVariable(label='Market Segment ID' required=true)
        public Id id;

        @InvocableVariable(label='Segment Name' required=true)
        public String name;

        @InvocableVariable(label='Segment Status')
        public String segmentStatus;

        @InvocableVariable(label='Publish Status')
        public String publishStatus;

        @InvocableVariable(label='Last Modified Date')
        public DateTime lastModifiedDate;

        @InvocableVariable(label='Last Segment Member Count')
        public Long lastSegmentMemberCount;

        // Constructor to map MarketSegment fields to DTO
        public MarketSegmentDTO(MarketSegment segment) {
            this.id = segment.Id;
            this.name = segment.Name;
            this.segmentStatus = segment.SegmentStatus;
            this.publishStatus = segment.PublishStatus;
            this.lastModifiedDate = segment.LastModifiedDate;
            this.lastSegmentMemberCount = segment.LastSegmentMemberCount;
        }
    }
}

Step 2: Create Agent action

To replicate this, copy and paste as an Apex class. and then create an action this time using Apex ; choose Invocable methods option.

Step 3: Test

After setting up the action as part of the segment topic, try out the agent:

Thanks for reading!

How useful was this post?

Click on a star to rate useful the post is!

Written by

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.