function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Saurabh Kulkarni 76Saurabh Kulkarni 76 

embedded_svc.liveAgentAPI.startChat doesn't work

Hello Guys,

I have been using embedded service for a while, I wanted to hide the standard button and start the chat with some custom button and for that I tried to use below code snippent. However, it gives me error all the time as Cannot read property "startChat" of undefined. If I print the object embedded_svc in the console, it comes fine. However, embedded_svc.liveAgentAPI gives and error. Can you please help what may have gone wrong?
 
embedded_svc.liveAgentAPI.startChat({
	directToAgentRouting: {
	buttonId: “573xx0000000000”,
	// userId: “”,
	fallback: true
},
extraPrechatInfo: [],
extraPrechatFormDetails: []
});

 
ShivankurShivankur (Salesforce Developers) 
Hi Saurabh,

Please check if you have missed out to implement the showWhenOnline correctly in your code snippet, which should work when an agent is online.

See similar issue discussed here:
https://salesforce.stackexchange.com/questions/114132/live-agent-issue-with-startchat

Hope above information helps, Please mark as Best Answer so that it can help others in the future.

Thanks.
Xi ShuangXi Shuang
Having the same issue, but ends up with the fact that this liveAgentAPI is only available after everything is initialized so you can't call startChat on pageload.

You can try something like to add a button which trigger a function that call startChat which should work.
p-obp-ob
We use this feature in our Live Agent deploy.

embedded_svc has an addEventHandler method that you can use to observe when everything is ready, and call startChat after it has been iniatilized (or after the availability check has been completed).

Something like:
function startChat() {
   window.embedded_svc.liveAgentAPI.startChat(/* your code here */);
}

// this must be configured BEFORE init
window.embedded_svc.addEventListener("onSettingsCallCompleted", () => startChat());

window.embedded_svc.init(/* your code here */);

You can read more about these events and what they are for here: https://developer.salesforce.com/docs/atlas.en-us.snapins_web_dev.meta/snapins_web_dev/snapins_web_chat_events.htm?search_text=onSettingsCallCompleted