July 27, 2020

How to close the Chat tab when the conversation is ended in Salesforce Chat in Lightning Experience?

lightning:conversationChatEnded Event is triggered when an active chat ends.
 
For the below component to work, 
1. Add it to the Chat Transcript Lightning Record Page.  
2. When the Chat is ended, lightning:conversationChatEnded event will be fired.
3. onChatEnded method will be called.
4. workspaceAPI.closeTab() closes the Chat Transcript record.
 
Sample Code:

ChatEnd.cmp:

<aura:component implements="flexipage:availableForAllPageTypes" access="global">
   
    <lightning:conversationToolkitAPI aura:id="conversationKit" />
    <lightning:workspaceAPI aura:id="workspace"/>
   
    <aura:handler event="lightning:conversationChatEnded" action="{!c.onChatEnded}" />
   
</aura:component>

ChatEndController.js:

({
   
    onChatEnded: function(cmp, evt, helper) {
       
        /*var conversation = cmp.find( "conversationKit" );
        var recordId = evt.getParam( "recordId" );
        console.log( "recordId:" + recordId );*/
        var workspaceAPI = cmp.find( "workspace" );
        workspaceAPI.getFocusedTabInfo().then( function( response ) {
            var focusedTabId = response.tabId;
            console.log( 'Focused Tab is ' + focusedTabId );
            workspaceAPI.closeTab( { tabId: focusedTabId } );
        })
        .catch(function( error ) {
            console.log( 'Error is' + JSON.stringify( error ) );
        });
       
    }
   

})


10 comments:

  1. Can you please guide me on SendMessage event how to customize it in lightning?

    ReplyDelete
    Replies
    1. Check this https://developer.salesforce.com/docs/atlas.en-us.api_console.meta/api_console/sforce_api_console_sendMessage.htm. I don't think there is event for this.

      Delete
  2. how can we use sforce_api in lightning?

    ReplyDelete
    Replies
    1. You can use it Classic button. You can use it in VF Page - https://developer.salesforce.com/docs/atlas.en-us.ajax.meta/ajax/sforce_api_ajax_vf_sample.htm?search_text=lightning

      Delete
  3. Can you add some details on how to fire this event

    ReplyDelete
    Replies
    1. Thanks for the feedback. I have added some instructions for better understanding of this component.

      Delete
  4. Hello ,
    i used the same code , if it works for 1 chat , if we have more than 1 chat(means 5 chats) , if customer is closed chat their end , popup window will show the End Conversion or Cancel to continue the same chat, if i click End Conversion one time that window will not close and still appearing and if i click 4 times then all the chats tabs closed automatically , can someone tell me the reason , its very urgent

    ReplyDelete
    Replies
    1. End user closing the chat window won't end the conversation. End user has to click End Conversation and confirm it.

      Delete
  5. Hi i did the same as what you suggested , but still getting the same error, see the below
    1) First i click on "Close dialouge"
    2) clicked Confirmed endchat
    3) Clicked Close Chat button

    In Console app , able to see the same screen "Cancel" & "End Conversation" , if i click "End Conversion" one time, confirmation screen is still appearing , then i start click 4 times then all the chat tabs closing and agent occupancy is updated to 0 , any suggestion please , its very urgent

    ReplyDelete
    Replies
    1. I am not sure about this.
      When the Chat is ended, lightning:conversationChatEnded event will be fired. Not sure, what is happening in this case. If the Chat is ended properly, definitely, the event will be fired. You can add some console.log statements in the component to check in the browser.

      Delete