[SalesForce] How to check if the record is already part of the opened primary or secondary tab

In the service console toolkit its not possible to open same record in two tabs since the tabs needs to be unique, reasons for which i understand. But i am in a scenario where in, on clicking a link in the VF page i am trying to directly open a new primary tab and getting an error as "Cannot open primary tab :Duplicate tab exists". So basically i need to check if that record is already part of any of the primary or secondary tabs and if so i need to focus on that tab instead of opening a new tab.

But in the toolkit developer guide i dont see any methods available to check if the tab is part of the console. How to i implement the logic of checking if the record is already opened and only if not open a new tab, is there a better way than this logic?

Best Answer

We have couple of built in method to get the already opened primary/sub tab ids. I have achieved this as mentioned below:

Let say we have an Account as primary tab and Contact as sub tab. First of all on click of each contact record will open the tab using openPrimaryTab() and collect object id and primaryTabId in an object.

And when user tries to open a already opened tab, will iterate over already opened sub tabs until we find it, once we get the selected subTab then will focus on it.

Example:

sforce.console.openPrimaryTab(null, '/'+contactId, true, '', function callBack(result){

    // On each success collect parent ObjectId and parentTabId in an Object
    if(result.success){
        map[accountId] = result.Id;

    }else{ // Control will come here when record is already opened

        //Iterate over above parentId to get its subTabId to focus
        for(var key in map){

            // passing parent tab id to get its sub tab ids
            sforce.console.getSubtabIds(map[key] ,function callBack(result){

                var sTabIds = result.ids;

                var isSubTabFound = false;

                // Iterate over all sub tab id until we find the selected record
                for(var i=0; i<sTabIds.length; i++){

                    (function(i){

                        // passing sub tab id to get its object id to compare and focus
                        sforce.console.getPageInfo(sTabIds[i], function callBack(result) {

                            if(result.success){

                                var sTabPageInfo = result.pageInfo;

                                if(sTabPageInfo.indexOf("/003") > -1){

                                    var getSubTabObjectId = sTabPageInfo.substr(sTabPageInfo.indexOf("/003")+1, 15);

                                    if(getSubTabObjectId == contactId){
                                        isSubTabFound = true;
                                        sforce.console.focusSubtabById(sTabIds[i]);
                                    }

                                }

                            } 

                        });

                    })(i)
                    if (isSubTabFound) break;
                }

            });
        }


    }
});
Related Topic