[SalesForce] How to know/find the Id or name of an already open primary tab in the console

How do I know/find the Id or name of an already open primary tab in the console?

We are building a console app where accounts are opened as primary tabs. Some of these primary tabs will be opened from a visualforce page which has a short list of relevant/related accounts and basically calls sforce.console.openPrimaryTab(). In these calls we don't specify a tab Id but we can specify a tab name (eg. the accountId) meaning any subsequent clicks on entries in the relevant/related accounts list will send the user to the correct tab as expected.

However accounts can also be opened from a standard account list. So we have no control of the tab name. What we then see is

  • Account X has been opened from the standard list

  • The user is on the tab for another related/relevant account Y

  • In the list of related/relevant accounts the user now clicks account X
    The call to sforce.console.openPrimaryTab() will fail with "openPrimaryTab: Opening a duplicate tab is not allowed."

How do we find the Id or name of this tab so we can send the user there?

Best Answer

You need the getFocusedPrimaryTabId, or possibly getPrimaryTabIds:

(function (c) {
    "use strict";
    var focusedPrimaryTabId, allPrimaryTabIds;
    c.getFocusedPriaryTabId(function (result) {
        focusedPrimaryTabId = result.id;
    });
    c.getPrimaryTabIds(function (result) {
        allPrimaryTabIds = result.ids;
    });
})(sforce.console);

Couple fun gotchas:

  • These methods do not return anything. You can only access their value from a callback.
  • Unlike Apex, Javascript properties are case sensitive, so result.Id won't work.