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
Madhanprabhu Thangadurai 1Madhanprabhu Thangadurai 1 

How to open multiple sub tabs under a single primary tab using a link click in service console ?

Hello All,

My requirement is to display multiple sub tabs under a single primary tab when a link is clicked. Here i have written a visualforce page to achieve the scenario. But I'm not able to achieve it. I can open only one sub tab and seems like the second sub tab is getting overridden with first sub tab. Thanks your help.
<apex:page standardController="Case">

    <A HREF="#" onClick="callMultipleSubTabs();return false">
        Click here to open a new subtab</A> 
    <apex:includeScript value="/soap/ajax/37.0/connection.js"/>
    <apex:includeScript value="/support/console/37.0/integration.js"/>
    <script type="text/javascript">
        var strURL;
        var strTitle;
        sforce.connection.sessionId = '{!$Api.Session_ID}';
        
        function testOpenSubtab(inputURL,inputTitle) {
            strURL = inputURL;
            strTitle = inputTitle;
            //First find the ID of the primary tab to put the new subtab in
            sforce.console.getEnclosingPrimaryTabId(openSubtab);
        }
        
        var openSubtab = function openSubtab(result) {
            //Now that we have the primary tab ID, we can open a new subtab in it
            var primaryTabId = result.id;
            sforce.console.openSubtab(primaryTabId , strURL, true, 
                strTitle, null, openSuccess, strTitle);
        };
        
        var openSuccess = function openSuccess(result) {
            //Report whether we succeeded in opening the subtab
            alert('Result'+result);
            if (result.success == true) {
                alert('subtab successfully opened');
            } else {
                alert('subtab cannot be opened');
            }
        };
            
            
        function callMultipleSubTabs()
        {
            //sforce.connection.sessionId = '{!$Api.Session_ID}';
            var caseIds = "5009000000VxOsE;5009000000VxOsD";
            var caseTitle = "00001001;00001000";
            
            var id = caseIds.split(";");
            var title = caseTitle.split(";")
            var baseUrl = "{!LEFT($CurrentPage.URL,FIND('/',$CurrentPage.URL,9))}";
            
            for(i=0;i<id.length ; i++)
            {
                if(sforce.console.isInConsole())
                {
                    //alert('In console');
                    var caseURL = id[i];
                    var caseTitleLbl = title[i];
                    //alert('Case URL'+caseURL);
                    
                    testOpenSubtab(caseURL,title[i]);
                    alert('Case URL'+caseURL);
                }
                else
                {
                    window.open("/"+id[i],'_blank');
                    window.focus();
                }
            }
            
            
        }
        

    </script>
</apex:page>

 
NagendraNagendra (Salesforce Developers) 
Hi Madhanprabhu,

Please find the below post which was addressed in the stack exchange community as below.
var function1 = function (inputValue) {
    unscopedVar = inputValue;
}
var function2 = function () {
    // use unscopedVar
}
Don't do that, it's bad practice. And in this case, it's not even going to work, because the variables are synchronous in function1 and asynchronous in function2. So the first step in getting your code working is to pass those variables to the openSubtab method.
var openSubtab = function (result, inputUrl, inputTitle) {
    // logic
}
However, you don't even need to make each call asynchronous. You can perform the asynchronous logic on page load.
(function (D, c) {
    var primaryTabId; // locally scope your variable
    D.addEventListener('DOMContentLoaded', function() {
        c.getEnclosingPrimaryTabId(function (tab) {
            primaryTabId = tab.id;
        });
    });
})(document, sforce.console);
Now you can remove that first line (and parameter) from openSubTab and make these calls synchronously on click.

So back to your loop. You are trying to pass a url of caseId, but that is not even a valid url. Make sure you prepend a / character to make it a relative path. I would recommend using the $Action variable but that might be overly complicated in this script. Since eventually, you will be iterating a List<Case>, I recommend designing your function to simply accept a Case record. Something like:
(function (D, c) {
    var baseUrl = "{!LEFT($CurrentPage.URL,FIND('/',$CurrentPage.URL,9))}",
        openTab = function (caseRecord) {
            var caseUrl = baseUrl + caseRecord.Id,
                caseTitle = caseRecord.CaseNumber;
            if (!c.isInConsole) {
                window.open(caseUrl, "_blank");
                window.focus();
            }
            else {
                c.openSubTab(primaryTabId, caseUrl /*other parameters*/);
            }
        }

    // include here DOMContentLoaded listener outlined above

})(document, sforce.console);
Best Regards,
Nagendra.P