How to invoke Embedded Service Chat on click of a button in Salesforce?

How to invoke Embedded Service Chat on click of a button in Salesforce?

embedded_svc.liveAgentAPI.startChat() can be used to invoke Embedded Service Chat on click of a button in Salesforce.

Check the following article for invoking from a Force.com site:

https://www.infallibletechie.com/2021/12/how-to-invoke-embedded-service-chat-on.html

Sample Code in VF Page:

<apex:commandButton value="Start Chat" onclick="embedded_svc.bootstrapEmbeddedService();" reRender="chatDetails"/>  

Sample code in HTML:

<html>
    <input type="button" value="Start Chat" onclick="startChat();"/><br/><br/><br/>
    <input type="button" value="Clear Chat Session" onclick="clearSession();"/><br/>
    
    <!--
        Snipped Code from Embedded Service Deployment
    -->
    
    <script>
        function startChat() {
            
            console.log( 'Inside Start Chat' );
            embedded_svc.liveAgentAPI.startChat({
                directToAgentRouting: {
                buttonId: "5735f000000Tpl7",
                fallback: true
            },
            extraPrechatInfo: [],
            extraPrechatFormDetails: []
            });
            
        }
        
        function clearSession() {
            
            console.log( 'Inside Start Chat' );
            embedded_svc.liveAgentAPI.clearSession();
            
        }
    </script>
</html>

Output:

Leave a Reply