Salesforce Custom Agent Actions using Apex REST
3K views
Mar 17, 2025
We can create custom Agentforce Agent actions using the Apex Rest Resource apex classes. This helps in reusing the existing Apex Development for the Agentforce Agent implementation. Blog Post: https://www.infallibletechie.com/2025/03/salesforce-custom-agent-actions-using-apex-rest.html
View Video Transcript
0:07
hello everyone in this video we are
0:09
going to see how to
0:11
create custom agent actions using your
0:16
existing
0:19
Salesforce rest resources in
0:25
Apex if you are using at rest resource
0:29
annotation in your city for s for your
0:31
Apex classes it means that using Apex
0:34
rest API we will be able to invoke these
0:38
Apex classes and methods from your third
0:42
party
0:44
applications if you wanted to reuse the
0:48
same class and method in agent
0:52
force then we can make use of agent
0:56
force Apex rest resources let's see how
1:00
to do
1:03
this we have to make use of Salesforce
1:06
agent force for developers if you go to
1:09
your Salesforce or and if you see agent
1:13
force for developers by default it will
1:16
be enabled if you have any legal
1:19
requirement to disable it you can
1:21
disable it if this is toggled on then it
1:25
will disable the agent force for
1:26
developers in this or so if this is
1:29
toggled on and
1:31
disabled um then you will not be able to
1:34
make use
1:36
of this custom agent actions using Apex
1:39
rest so make sure this particular
1:43
disabled agent force for developers in
1:46
this orc
1:47
is toggled off so it is enabled so this
1:52
is
1:54
done then we have to install agent force
1:58
for developer V code extension in your
2:01
vs code editor once you install you will
2:05
see the agent force icon here using
2:08
which we can make use of the agent force
2:14
assistant in order to get the vs code
2:16
extension link please go to this blog
2:19
post I have given step bystep
2:22
instructions using which you should be
2:24
able to install Salesforce agent force
2:27
for developers extension in your vs code
2:30
editor and then you should be able to
2:32
use
2:33
it next step is you have to run this
2:37
code or you have to run this command you
2:40
can go to your vs code terminal and you
2:43
can run or you can open your sfdx uh um
2:47
command line interface and then you can
2:49
run it if you run it in the
2:51
terminal you will see files deleted and
2:55
and it will recreate the files the
2:57
advantage of running this command is it
2:59
will help in generating open API V3
3:04
specification in a yaml editor in the vs
3:07
code
3:08
ID if you have worked in moft or any
3:12
other Technologies which makes use of yl
3:16
yaml we say it as yaml so you you will
3:20
be familiar with it um it makes use of
3:24
uh
3:25
indentation uh in the code very similar
3:28
to python uh so you if you're are not
3:31
familiar you can uh search in uh um moft
3:36
help document and then you should be
3:37
able to understand more information
3:39
about yl once this code is uh sorry once
3:42
this command is successfully
3:46
executed you can go to sfdx iph project.
3:50
Json it
3:53
will change or add the source Behavior
3:56
options as decompose external Service
3:58
registration beta
4:00
so if you go here if you go to sfdx
4:05
project. Json it changed it or it added
4:08
it so I run this
4:12
uh command in my vs Cod terminal so it
4:16
got
4:17
updated so step three is also done next
4:21
step is we have to execute the following
4:23
command you can copy this
4:28
command and then uh now if you do
4:30
control shift
4:32
B here you should be able
4:34
to uh search for
4:40
it so this will refresh all the s object
4:44
references uh in
4:47
Salesforce so once this is also
4:50
done you have to select all s object
4:53
options when you run this
4:55
command now um vs code is set up uh all
5:00
the objects reference are refreshed
5:03
because we run this particular
5:07
command so this is also
5:10
done next you have you will already have
5:13
an existing Apex class or if you wanted
5:16
to create and use it for multiple
5:18
purpose also you can create make sure
5:20
you your class have with sharing keyword
5:22
or without sharing or inherited
5:25
keyword uh any one of those uh keywords
5:28
should be there in your
5:30
class and U it should have any one of
5:33
these annotated method it can be get
5:36
post put patch or delete so make sure it
5:39
has this is a sample
5:41
class uh this is not a working code but
5:44
I got this from sales force reference
5:46
document so you can also make use of
5:48
this just to play around so once this
5:51
code is uh created in your uh vs code uh
5:55
editor you can deploy it or you can keep
5:57
it local as of now next you have to run
6:01
this uh
6:03
command uh you can right click your Apex
6:06
class and then you can run this
6:10
command if you prefer to use count
6:14
control Shifty then you can also make
6:16
use of that so here you can
6:21
do command shift B and then you can
6:24
paste that and then you should be able
6:26
to run it or you should be able to yeah
6:30
I cannot do it from here but I should be
6:32
able to do it from the Apex class so you
6:34
can right click the Apex class and then
6:36
you should be run it or you should open
6:39
the Apex class you have to do command
6:41
shift p and then you should be able to
6:45
run
6:47
this so this will
6:49
create external Service registration iph
6:52
meta.xml file so this is the file which
6:56
is automatically created from this Apex
6:58
class uh
7:00
so here I am making use of a simple HTTP
7:04
post method annotation in this um using
7:08
this annotation I am having a method
7:10
called import case the import case
7:13
accepts subject and description it is
7:15
creating a case record once the case is
7:18
successfully created it will say case
7:20
created for you if there are any DML
7:23
exceptions it will return seeing that um
7:26
e E.G message will return the failure
7:29
message for the insert uh case operation
7:32
and then it will return the failure
7:33
message back to the agent force uh
7:36
action so this is the class I have made
7:40
use of it when I ran this create open
7:43
API document from this class beta it
7:46
generated this file automatically but
7:49
when I tried to use it it didn't work
7:52
there was an issue so you also make sure
7:54
you are checking this in the
7:56
responses in the content by default it
8:00
was using
8:01
application uh SL Json but if you look
8:05
at the return type of my Apex um method
8:09
it is rning string so I changed the
8:12
content type from application iph Json
8:15
to text this one was automatically
8:18
generated by Salesforce so this worked
8:21
so instead of application uh SL Json
8:24
when I changed it to text I deployed
8:27
this to the arc you have you have to
8:30
deploy the class also if you haven't
8:32
deployed so deploy both Apex class
8:34
metadata and external Service
8:36
registration I
8:39
meta.xml metadata type once it is
8:43
deployed you can go
8:46
to API
8:49
catalog and then you should be able to
8:51
verify it go to apa catalog in
8:54
Salesforce setup go to Apex beta you
8:57
should see your Apex class
9:00
and you should also see the
9:06
method next I
9:08
created agent action using the Apex
9:12
class so this is the um agent action I
9:17
created you can also go to agent actions
9:19
you can click
9:28
new so here you should be able to select
9:31
it since I have already created I'm not
9:33
going to duplicate
9:34
it so this is the agent action which I
9:41
created and
9:44
uh this is my agen for service agent I
9:48
created a topic called create case this
9:51
is just
9:52
for uh demo purpose so I didn't
9:55
configure it with the proper
9:57
classification scope and instruction
10:00
and I imported the agent
10:05
action by making use of add from asset
10:08
Library if you haven't created the agent
10:10
action you can also create it from here
10:13
so this is the agent action I'm making
10:15
use of it from the Apex rest
10:18
resource now let's test it I wanted to
10:21
directly test it so I'm going to say
10:24
create the case and this this is the
10:26
subject and
10:28
description before before we execute as
10:30
of now there are no cases in my
10:40
arc one more important thing go to
10:43
settings find this
10:46
user for this user you have to give
10:49
access to this Apex class else it won't
10:51
run that is also very important
10:55
step so it created the cas successfully
10:58
for me
10:59
let's verify that yeah subject is test
11:04
it is created by the Einstein service
11:06
agent
11:07
user and
11:12
uh description is demo subject is
11:17
test so I was
11:19
easily able to reuse my existing Apex
11:23
rest resoures in Salesforce agent force
11:27
action and I was able bble to reference
11:29
it in my topic and then I was able to
11:32
execute
11:35
it please check the video description in
11:38
the video description I have given my
11:40
blog post from the blog post you should
11:42
be able to get the commands sample Apex
11:46
code um for your reference and then you
11:49
should be able to play
11:56
around I hope it was helpful
12:04
thank you for watching
#Internet Software