[SalesForce] Cloud Console: How to refresh primary tab from a subtab

I've been trying to refresh a primary tab from a subtab with no luck, and would like a working example. According to their doc, they do not give an example of how to retrieve your primary tab's id from a subtab. How do you retrieve this information without knowing the Id from using something like openPrimaryTab?

Doc: https://developer.salesforce.com/docs/atlas.en-us.api_console.meta/api_console/sforce_api_console_refreshprimarytabbyid.htm?language=en

 <A HREF="#" onClick="testRefreshPrimaryTabById();return false">
     Click here to refresh a primary tab by id</A> 

<apex:includeScript value="/support/console/22.0/integration.js"/>
<script type="text/javascript">
    function testRefreshPrimaryTabById() {
        //Get the value for 'scc-pt-0' from the openPrimaryTab method
        //This value is for example purposes only
        var primaryTabId = 'scc-pt-0';
        sforce.console.refreshPrimaryTabById(primaryTabId, true, refreshSuccess);
    }

    var refreshSuccess = function refreshSuccess(result) {
        //Report whether refreshing the primary tab was successful
        if (result.success == true) {
            alert('Primary tab refreshed successfully');
        } else {
            alert('Primary did not refresh');
        }
    };

Example code I've tried out:

    var closeSubtab = function closeSubtab(result) {
        var tabId = result.id;
        sforce.console.closeTab(tabId);
    };

    var primaryTabId = function showTabId(result) {
        //Display the tab ID
        //alert('Tab ID: ' + result.id);
    };

    var refreshSuccess = function refreshSuccess(result) {
        //Report whether refreshing the primary tab was successful
        if (result.success == true) {
            alert('Primary tab refreshed successfully');
        } else {
            alert('Primary did not refresh');
        }
    };

    function closeTab() {
        sforce.console.refreshPrimaryTabById(primaryTabId, true, refreshSuccess);
        sforce.console.getEnclosingTabId(closeSubtab);
    }

    function exitWizard(){            
            if(sforce.console.isInConsole()){                                  
                closeTab();
            }else{
                window.location.href = '/' + recordId;   
            }
        }
    }

    exitWizard();

Best Answer

I finally was able to get this code working...

The documentation does not explain this well in my opinion. Basically, you create a variable, in this case called refreshPrimaryTab which is assigned to a function which will retrieve your primaryTabId and assign it to your request for a refresh. This though will not execute yet. In order to execute, you need to call a salesforce function. This is done by calling sforce.console.getEnclosingPrimaryTabId() with your variable inside. As the example shows it will look like: sforce.console.getEnclosingPrimaryTabId(refreshPrimaryTab);

Below is a simple example of how from a sub tab you can get your primary tab to refresh.

    var refreshPrimaryTab = function showTabId(result) {
        var tabId = result.id;
        sforce.console.refreshPrimaryTabById(tabId, true);
    };

    function doPrimaryTabRefresh() {
        sforce.console.getEnclosingPrimaryTabId(refreshPrimaryTab);            
    }

    doPrimaryTabRefresh();
Related Topic