Salesforce Agentforce: Running Pre Reasoning Actions in Agent Script
Apr 15, 2026
Show More Show Less
View Video Transcript
0:07
Hello everyone. In this video, we are
0:09
going to see how to execute
0:12
Apex agent action
0:16
using agent script in the new Salesforce
0:19
agent force builder
0:21
before
0:23
the sub agent uh topic selector picks
0:27
the right topic.
0:30
The use case uh what we are going to see
0:32
in this video is
0:35
whenever a chat is initiated
0:39
and uh if the user prompts for request
0:42
in the chat
0:45
in the agent force it will execute the
0:49
topic selector sub agent and then it
0:52
will try to find the right
0:54
uh sub sub aent and then it will try to
0:58
execute the action. s related to it.
1:02
My use case is before it picks the right
1:05
uh sub aent I wanted to check whether
1:09
the request is within the business
1:11
service. If the request is within the
1:13
business service then
1:17
it will find the right sub agent. Let's
1:20
say the request is made outside the
1:24
business service then it will send a
1:27
message saying that please call our 1800
1:32
number for 24 bar 7 support since you
1:34
are reaching out to the chat which is
1:36
outside the business hours. So this is
1:38
my use case.
1:41
The apex class used for this is
1:49
so this is my apex class business hours
1:52
controller here I'm querying business
1:55
hours object or entity where the name is
1:58
default
2:00
with the help of business hours within
2:03
uh method I'm passing the business uh
2:07
hours id and the current date and
2:11
Using this method, I'm finding out
2:13
whether the request is made within the
2:15
business hours or not. If it is within
2:18
the business hours, then it will send a
2:20
message saying that the request is
2:22
within the business hours. So, this is
2:23
the message that will be sent to the uh
2:26
agent action from the apex class. If it
2:28
is outside the business hours, then it
2:30
will say I cannot transfer now. It is
2:32
outside of business hours. For
2:34
emergency, call our 24 bar 71 1800
2:38
number. So this is the message that will
2:40
be sent from the apex agent action so
2:42
that the LLM will read the information
2:45
and uh display accordingly on the chat
2:48
conversation.
2:54
My sub agent here is topic selector. So
2:58
this is the first sub aent that will be
3:01
triggered when a request is made in the
3:03
chat. And if the chat is connected to
3:06
the service agent then it will trigger
3:09
this sub agent.
3:12
In this uh topic selector sub agent
3:16
I am making use of before reasoning
3:18
block. In this before reasoning block I
3:22
am running business hours check a apex
3:26
action. Here I'm passing the record ID
3:28
as a rootable ID. If it is a messaging
3:31
session it will pass the messaging
3:32
session record ID. If it is from the
3:34
voice then it will uh pass voice call
3:38
record ID. If you wanted to do some kind
3:41
of uh logging information then you can
3:44
add your logic here.
3:49
Okay. Next
3:52
this will uh set the variable business
3:54
hours message and is within business
3:57
hours. These two are the variables I
3:59
have declared based on the apex
4:03
execution. Whatever result it is sending
4:06
the message and as well as whether it is
4:07
within the business hours or not, the
4:09
boolean value is sent from the apex to
4:12
these variables.
4:15
Okay. Next only it will execute the
4:18
reasoning in um uh block. So in the
4:20
reasoning block I'm saying that please
4:23
check if at variables do is business
4:26
hours equal to equal to true. If it is
4:29
true then select the best tool to call
4:32
based on the conversation history and
4:34
the users intent. Else show the message
4:37
that is sent from the apex
4:40
um agent action. It will not directly
4:43
print the message that that is uh sent
4:46
from the apex. the LLM will try to
4:50
interpret and then uh um try to display
4:54
accordingly. It won't exactly give that
4:56
particular information. If you want to
4:58
just directly display the instruction um
5:01
sorry the message that was sent from the
5:03
apex agent action then you have to add
5:06
the instructions clearly here saying
5:08
that do not change or do not interpret
5:10
any me information that was sent from
5:12
the apex agent action. So that the exact
5:15
message that was sent from the apex
5:17
agent action is displayed on the chat
5:19
conversation
5:22
and um business hours um check is the
5:27
action
5:29
which I have configured. So this is my
5:32
apex
5:34
agent action.
5:37
So the the the referenced action type is
5:40
apex. It is an invocable method.
5:42
Uh the method name is uh business hours
5:46
check.
5:51
I am uh the inputs I have only one input
5:54
list of record ids. Um and then uh for
5:57
the output
6:00
I have two but not sure why it is not
6:03
showing.
6:10
Yeah, maybe since I have committed it,
6:12
it is not uh
6:17
it is not showing the output but in the
6:19
output
6:21
I will be able to get string message and
6:24
as well as a boolean variable is within
6:26
business hours. So these two values are
6:29
mapped
6:38
to the variables that I have declared in
6:41
the agent script.
6:44
So now if I initiate a chat if I send a
6:47
message if it is within business service
6:49
then it will choose the right uh tool
6:52
and then it sorry right uh sub agent and
6:55
then it will try to execute the
6:56
instructions and the actions that are uh
6:59
declared in that sub agent. Let's say if
7:02
I am trying to uh start a conversation
7:05
which is outside of my business hours
7:08
then it will interpret the message that
7:10
was sent from the apex agent action and
7:11
then it will display.
7:14
So in my Salesforce or I have said for
7:19
all 7 days the business hours are 9:00
7:22
a.m. to 10:00 a.m. Now it is outside my
7:25
business hours. So this is my embedded
7:29
service uh deployment.
7:32
Let me quickly test it.
7:35
So in the agent builder, if I go to
7:37
canvas.
7:39
So
7:42
So this one is tied to enhanced chat v2.
7:46
So this is the enhanced chat v2 embedded
7:49
service deployment. I'm going to test it
7:52
now.
7:53
Let's wait for the agent for service
7:55
agent to connect.
8:00
Okay, service agent joined. So now it
8:03
should send the welcome message. The
8:05
welcome message is u sent. I can also
8:08
make use of voice icon here so that I
8:10
can use it in the voice mode.
8:18
Okay, it is not working for some reason.
8:21
Okay, how to cancel my order? Now it
8:26
will go here.
8:29
It will try to find the start agent
8:33
block and then it will try to execute
8:34
this topic selector sub agent. It will
8:37
run the business hours check. It will
8:39
set the variables and then it will check
8:41
whether it is within business hours. If
8:43
it is since it is not within the
8:45
business service, it won't run any of
8:47
these actions because if you look at all
8:49
these uh um actions, it will run only
8:54
when the variable is business is equal
8:56
equal to true. If it is false, then it
8:59
won't be able to pick any of this
9:01
actions
9:03
and then it will just show the message.
9:08
So make use of uh before reasoning and
9:11
as well as available when in the agent
9:14
script so that the actions are not
9:16
getting executed and it will be executed
9:19
only when is within business hours of
9:21
boolean variable is set to true
9:24
how to cancel my order.
9:30
Okay, it appears that order
9:31
cancellations cannot be processed now
9:33
because it is outside our business
9:34
hours. for urgent assistance please call
9:36
our 247 hotline at 1800. So it
9:43
if it if I
9:46
okay if I'm if I don't have this
9:48
available when at variables is within
9:50
hours equal to true then it would have
9:53
picked general FAQ and from the general
9:56
FAQ it would have picked answer
9:59
questions with uh uh knowledge action
10:02
and then it would have made use of uh
10:07
the data library
10:09
and then then it would have made use of
10:10
the search index and the retriever so
10:13
that it it would have fetched the right
10:15
information and then it would have
10:16
displayed uh to the customer but my use
10:18
case was I don't want to involve agent
10:20
force agent outside my business service
10:29
please check the video description in
10:31
the video description I have shared my
10:32
blog post from the blog post you should
10:34
be able to get the sample apex class and
10:38
as well as I have shared my entire uh
10:40
agent script for your reference.
10:55
I hope it was helpful.
11:05
Thank you for watching.
#Internet Software
