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
Chris VogeChris Voge 

Service Cloud - addEventListener for opening/closing a tab

I created a couple of javascript functions for managing the number
of open tabs inside the service cloud console. I am using addEventListener
for the OPEN_TAB and CLOSE_TAB console events to trigger these functions.

I was also assuming clicking the "+" button for explicitly opening a new tab as well
as closing it would also trigger my functions. The function associated with the
OPEN_TAB was NOT triggered, but the function associated with the CLOSE_TAB
DID get triggered. Not sure why.

I am using the integration.js library, version 30. I tried 31, but it wasn't working
for some reason.

Any help would be appreciated. Thanks, Chris
logontokartiklogontokartik
HI Chris,

Can you please post the code so that we can take a look? It should work for both OPEN_TAB and CLOSE_TAB events the same way, unless there is something I am missing here.

Thank you.
Chris VogeChris Voge
Here's some of the javascript from my Visualforce page:

<script src="/support/console/30.0/integration.js" type="text/javascript"></script>
<script type="text/javascript">
                var openTabs = 0;
                
                window.onload = function(){
                    var numberOfResults = '{!customerSearchResults.size}';
                    if(numberOfResults == '1'){
                        openCustomerPTab('{!firstCustId}', '{!firstCustName}'); 
                    }
                    sforce.console.getPrimaryTabIds(sumNumOfTabs);
                    sforce.console.addEventListener(sforce.console.ConsoleEvent.CLOSE_TAB, decrementNoOfTabs);
                    sforce.console.addEventListener(sforce.console.ConsoleEvent.OPEN_TAB, incrementNoOfTabs);
                }

                var sumNumOfTabs = function sumNumOfTabs(result) {
                    if(result.ids.length > 0){
                        if(result.ids[0] != ''){
                        	openTabs = result.ids.length;
                        }
                    }
                };

                function decrementNoOfTabs(result) {
                    var fld1 = result.id;
                    if(fld1.indexOf("st") > -1){
                    	 openTabs--;
                    }
                }
                
                function incrementNoOfTabs(result) {
                    var fld1 = result.id;
                    var fld2 = result.objectId;
                    if(fld1.indexOf("pt") > -1 && fld2 == "undefined"){
                    	openTabs++;
                    }
                }
                
                function openCustomerPTab(customerId, customerName) { 
                    if(openTabs >= 4){
                        alert("Reached Maximum Number Of Open Tabs");
                    }else{
                        sforce.console.openPrimaryTab(null, '/apex/SD_CustomerDetails?customerid='+customerId, true, 
                            customerName, 'CustomerDetailsTab');
                    }
                }
</script>
Further things that I have noticed:

       - Function incrementNoOfTabs() is triggered when opening a new tab as well as clicking on a tab
         that had not as of yet received focus. The difference between the 2 is the objectId from the result
         object. If opening a brand new tab, the objectId is set to "undefined". Of focusing on a tab, the objectId
         is set to spaces.

       - I am dealing with only primary tabs at this point. When closing a tab, decrementNoOfTabs() is called
         just fine. But the Id in the result object is set as a subtab ID. 
         IE. scc-st-#        as opposed to       scc-pt-#
         
         That's why I'm keying off of 'st' instead of 'pt'

Let me know if you need further info

Thanks, Chris

Chris VogeChris Voge
Another thing I noticed is sometimes the increment function is getting called
twice. Once containing a primary tab Id  ( scc-pt-# ) and once containing a 
subtab id ( scc-st-# ). And, as I mentioned earlier, I am only using primary
tabs.

Another question I have ( separate topic ) is is there a way to somehow disable
or hide the "+" button for adding a new tab ?

Thanks for your help.

Chris
Chris VogeChris Voge
I am using the Google Chrome browser
logontokartiklogontokartik
Hi Chris,

You are correct. I think there is some issue with the Standard Events OPEN_TAB & CLOSE_TAB and they seem to be not working as they are supposed to. Since these events are just one release old, I think no one really figured out the issue or opened at ticket.  I will see what I can do and in meantime he is summary of what I am noticing.

1. Clicking + is not triggering the OPEN_TAB event. 
2. Opening a tab (via any record) is calling the listener's method twice, once for Primary Tab open and once for Sub Tab open.(Detail)
3. Closing the Tab is firing CLOSE_TAB event everytime. 


Thank you
Kartik


Chris VogeChris Voge
Hi logontokartik,

I mentioned earlier that I couldn't get the version 31 javascript
toolkit library to work. The one using the relative URL /support/console/.....

The third party option:
<script src="https://c.na1.visual.force.com/support/console/31.0/integration.js" type="text/javascript"></script>

was available to use.

It appears clicking '+' is still not triggering the OPEN_TAB event
in version 31. Close tab is getting fired. So appears to still be an
issue in version 31.

Wanted to let you know.

Thanks, Chris


Chris VogeChris Voge
Anyone looking into this ? I'd appreciate it. Thanks !!
Debmalya BanerjeeDebmalya Banerjee
I am having the same issue. What I have observed till now is that OPEN_TAB event does not  get fired for the first Primary Tab which is Opened. I am at a loss to find out why. For all the rest oif the Primary Tab and Subtabs the Event sometimes gets fired twice (not consistently), though that is something I can live with. Currently the main issue I am facing is that since the Open_Tab does not get fired for the first Primary Tab, I am unable to capture the TabId or the Object Id for the first tab. I tried getEnclosingtabid(), but i am not aboe to capture the value in my local variables as the calls are asynchronous.
KrForceKrForce
Hi Chris, 
Did you figure it out any solution, im in same boat now. If you found any alterate solution, can you please share?
sharathchandra thukkanisharathchandra thukkani
You can try below code, it will work

<apex:page showHeader="false" sidebar="false" standardStylesheets="false" applyHtmlTag="true" applyBodyTag="true" docType="html-5.0">
<html xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">

<head>
    <title>ChatAlert</title>
    <apex:includeScript value="/support/console/40.0/integration.js"/>
    <apex:includeScript value="{!URLFOR($Resource.jquery_ui, 'js/jquery-1.6.2.min.js')}" />
</head>

<body>
</body>

<script>
$(document).ready(function(){
/*
 sforce.console.addEventListener(
            sforce.console.ConsoleEvent.PRESENCE.WORK_ACCEPTED,
            function (result) {
                console.log('Accepted work item: ', result);
                alert(result.workItemId);
            });
*/
sforce.console.addEventListener(
            sforce.console.ConsoleEvent.CLOSE_TAB  ,
            function (result) {
                console.log('Closed item', result);
            });
          
});
</script>
</html>
</apex:page>
Ranganath C NRanganath C N
Hi all,
I am new to salesforce and my requirment is to creating listener to create custom keyboard shortcut to Close All Tabs in the service console.
Can any one help me to write listener class ..
Thanks