I am trying to implement opensubtab functionality. Below is my code:

<apex:page id="pg" controller="testSetRedirect">

<apex:includeScript value="/support/console/41.0/integration.js"/>
<script type="text/javascript">

    function testOpenSubtab() {
        //First find the ID of the primary tab to put the new subtab in
    var subtabid='';
    var primaryTabId;
    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;
        var cs = '5007F000001DFt9';
        console.log('Second console log'+subtabid);
        sforce.console.openSubtab(primaryTabId , '/'+cs, true, 
            'salesforce', subtabid, openSuccess, null);
        console.log('Third console log'+subtabid);
    var fetchsubtabId = function fetchsubtabId(result){
        subtabid = result.id;
        console.log('First console log');

    var openSuccess = function openSuccess(result) {
        //Report whether we succeeded in opening the subtab
        if (result.success == true) {
            alert('subtab successfully opened');
        } else {
            alert('subtab cannot be opened');
<apex:form >
    <apex:commandbutton onclick="testOpenSubtab();" value="testclick"/>

I have am facing the following issues on trying to open the subtab by clicking testclick button:

  • Why am I getting console logs in the order :Second,Third and First. Shouldn't callback method fetchsubtabId get executed ahead of the 'Second Console log'.

  • Why is the openSuccess callback not firing?

  • On clicking the button subtab opens in new tab(may be due to subtabid being null). But on closing the newly opened subtab and clicking the button existing subtab gets overriden.

  • On adding rerender = "pg" to the button openSuccess callback fires.

1 Answer 1


The sforce.console.getEnclosingTabId method is asynchronous and the method you pass in as a parameter is termed a callback function. This behavior is clearly indicated in the documentation:

This method is asynchronous, so it returns its response in an object in a callback method.

Due to the above, your subtabId is still empty (''), so your call to openSubtab likely fails. If you want to open a new subtab, use null instead of an empty string.

The getEnclosingPrimaryTabId method is also asynchronous, as are most in the Console Integration toolkit.

Please note also that you lose most of the benefit of the var functionName = ... sytnax when you also put the name after the function, as that adds its definition to the global scope.

Instead of:

var myFunctionName = function myFunctionName(parameters) {


var myFunctionName = function (parameters) {
  • Thanks , that answers the order of the logs. If you could also tell about the openSuccess not firing and subtab getting overridden when button is clicked send time after closing the new subtab.
    – starhunter
    Commented Jan 5, 2018 at 21:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .