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 ) );
        });
       
    }
   

})


19 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
  6. Replies
    1. Test it by ending the conversation. visitor closing their browser window doesn't end the chat. They have to click End Conversation button to end the Chat.

      Delete
  7. Thank you for the response!
    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.

    ReplyDelete
    Replies
    1. Interesting. I never faced this issue. I have used the same code.

      Delete
  8. When there is only one chat open, it works fine. But when there is multiple chat tab open, I face the issue which I mentioned at the previous comment, that is it will pop up the confirmation message again and again.

    ReplyDelete
    Replies
    1. I am getting "Closing this tab will also end the conversation. Click 'Cancel' to continue your conversation." only when I close the Chat window instead of clicking End Chat button.

      Delete
  9. Hi! I have implemented this feature but it doesn't work with messaging session conversation.
    Do you know why it doesn't work appropriately?

    ReplyDelete
    Replies
    1. Currently, it is not available for messaging.
      https://trailblazer.salesforce.com/ideaView?id=0873A000000Lrc8QAC

      Delete
  10. Hi guys! @hanu and @sow this occurs with you guys because you need to get the TAB ID correctly.
    This function workspaceAPI.getFocusedTabInfo just get de tab that is open on the console.
    So you need store RECORDID and TABID in a hashMap.

    When the chat was ending by you or by customer you need get recordId and retrieve the related TabId that was stored in the HashMap.

    example:

    CONTROLLER.js

    init : function(component, event, helper){
    var myMap = new Map();
    workspaceAPI.getAllTabInfo().then(function(response) {
    response.forEach(function(item){
    myMap.set( item.recordId.substring(0,15), item.tabId);
    });
    }
    component.set('c.var_map', myMap );
    },

    onChatEnded: function(cmp, evt, helper) {

    let myMap = cmp.get('c.var_map');

    var recordId = evt.getParam( "recordId" );

    console.log( "recordId:" + recordId );
    let idTab = myMap.get( recordId );

    var workspaceAPI = cmp.find( "workspace" );
    workspaceAPI.closeTab( { tabId: idTab} );

    }


    BUT REMEMBER you need repopulate your VAR_MAP when a new tab was open.

    ReplyDelete