Salesforce Agent API from Lightning Web Component
4K views
Feb 24, 2025
We can securely and seamlessly access the Salesforce Agent APIs from Lightning Web Component using Apex. Blog Post: https://www.infallibletechie.com/2025/02/salesforce-agent-api-from-lightning-web-component.html
View Video Transcript
0:05
hello everyone in this video we are
0:07
going to see how to access Salesforce
0:11
agent API from lightning web
0:14
component to securely access Salesforce
0:17
agent
0:18
API for lightning web component
0:21
purpose I'm going to make use of
0:26
Apex in order to store the consumer key
0:30
consumer secret my domain URL and AP
0:33
instance
0:35
URL I have made use
0:37
of custom metadata type so this is an
0:41
org where I'm going to create a
0:43
lightning web
0:45
component and access Salesforce agent
0:47
API via Apex and display it on the
0:50
lightning web
0:53
component the ARG where agent force
0:56
agent is
0:57
configured the Connected app for that
1:00
particular
1:01
Org the consumer key and consumer secret
1:04
from the Connected app all this
1:07
information are stored in this custom
1:09
meta data
1:12
type this is
1:14
the API instance
1:16
URL and this is the
1:18
Salesforce my domain URL where the agent
1:23
force agent is hosted since I'm going to
1:26
make uh HTTP requests I'm going to to
1:31
allow list those uh end
1:34
points in my remote site
1:37
settings so the
1:39
configuration information is stored in
1:43
custom metad data type in order to
1:46
securely access the end points whenever
1:50
I'm making HTTP request I have all
1:52
listed in remote site
1:54
settings let's dive into the demo this
1:58
is the lightning web component where I'm
2:00
going to invoke the agent force agent
2:02
from a different Salesforce or I'm going
2:05
to say
2:06
interested in
2:09
products now it will go since I'm
2:12
starting the conversation it will go do
2:15
a post request get the access token next
2:18
it will do a another post request and
2:22
get the session ID next it will
2:26
send and uh it will send uh the message
2:30
to the agent force agent so this is the
2:33
response I got
2:38
back I would like to buy
2:43
products let me see now it won't again
2:47
get the access token create the session
2:49
ID it will make use of the existing
2:51
access token and session
2:54
ID okay so I was able to get the
2:57
response back here are the products I
3:00
would like to know more about this test
3:02
one product so I'm going to say I would
3:04
like to know more about this
3:18
product I'm going to send it
3:28
again okay so this is the product image
3:32
and it is asking whether I would like to
3:35
purchase it so I can continue the
3:38
conversation uh using this lightning web
3:40
component once I click end session this
3:43
download icon will be available you can
3:46
click it and then you should be able to
3:48
download the conversation transcript as
3:51
a text
3:53
file let's do a quick code walk through
3:56
I'm not going to Deep dive into the code
3:59
but High LEL I will say what is
4:03
developed this is the Apex
4:07
class where I am uh uh sending the
4:10
message so whenever they click Send
4:13
message it will pass this it will call
4:16
this method it will pass the token when
4:20
it was issued the session ID and string
4:23
message when the token is
4:27
blank or if the last token issued is
4:31
blank I will try to do a request and get
4:37
the access token so this is the
4:40
method uh which you do a post request
4:43
and get the access token so I'm storing
4:45
the access token and sending it back to
4:47
the lightning web
4:51
component next I'm checking whether the
4:54
session ID is blank if the session ID is
4:56
blank I am making a session request this
4:59
is another post request to get the
5:03
session
5:05
ID once that is
5:13
done I am doing another post request to
5:17
send the message uh there are two end
5:20
points to send uh messages to the agent
5:23
force agents one synchronous and the
5:26
other one is streaming uh endpoint I'm
5:29
making use of synchronous
5:31
endpoint so this is the method which do
5:35
the post request to send the message
5:38
once it process I will get the HTTP
5:41
response I will process it and then I
5:44
will send it back to the lenning web
5:48
component this is the lightning web
5:50
component
5:51
HTML where I have download utility
5:54
button
5:55
icon I'm displaying all the messages
6:00
using lightning text area I'm getting
6:03
the information that has to be passed to
6:05
the agent force agent two buttons one to
6:08
send the chat information and the other
6:11
one is to end the
6:16
session yeah whenever they click chat it
6:19
will call this method it will call that
6:22
send message method in Apex and then it
6:25
will try it will um get the access token
6:29
get the session ID and also it will do
6:31
the um synchron it will do a post
6:34
request to the synchronous endpoint to
6:37
process the message and then it will
6:39
receive the message back from the agent
6:40
force
6:42
agent next time when we call the same
6:45
method it will again pass the access
6:47
token and session ID since they are not
6:50
blank it will not go and do another set
6:53
of post requests to get the access token
6:57
and session ID it won't do those it will
7:00
directly go to the third HTTP post
7:03
method where it will try to send the
7:05
syron
7:08
message whenever the message is uh
7:11
entered I am storing that information in
7:13
the St strr message in the handle
7:16
session whenever they click end session
7:18
I'm calling this uh end session Apex
7:22
class method to end the
7:24
session so this is the code which I'm
7:27
making use to download the
7:31
conversation in the end session this is
7:34
a method from the Apex class chart
7:37
client
7:41
controller so here I'm making use of
7:45
a delete
7:47
request so that the session will be
7:50
ended it is always a best practice once
7:54
the conversation is done we have to end
7:57
the session immediately
8:06
please check the video description in
8:08
the video description I have given my
8:10
blog post from the blog post you should
8:12
be able to get the sample Apex code and
8:16
the lightning web component code for
8:18
your
8:25
reference I hope it was helpful
8:34
thank you for watching
#Intelligent Personal Assistants
#Internet Software