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

Primary Tab refreshes in console and Sub tab (VF page) reopens again after Refresh

I have an issue. My requirement is this, I have a VF page which opens from a custom button located on a Case Detail page. 
The VF Page has a CommandLink and a CommandButton which performs certain update to the Case Owner field on the Case detail page. 
The VF Page is opened as a Sub tab to a Case Primary tab on the console. 

After updating the changes to the owner field on the case detail, I have to refresh my Primary Tab(Case Detail) and along with that i have to close the Sub tab which contains the VF Page.
I find a weird issue here, 
My sub tab is getting closed first and the Primary tab refreshes after sub tab is closed. So what happens now is wierd for me, When the refresh of the primary tab happens it opens the closed VF Page again(that is the sub tab is reopened).
I want to close the sub tab after the save has happened on the primary tab and refresh success. 

Here is my code, 

<apex:page standardController="Case" extensions="CIAssignProjectController" name="Assign Project">
    <apex:includeScript value="/support/console/34.0/integration.js"/>

    <script type="text/javascript">

        function setBeforeUnloadEvent(flag) {
            console.log('getting primary tab id');
            console.log('getting current subtab id');
                //First find the ID of the current tab to close it
        function refreshTabsAfterSave () {
            console.log('getting link');
            sforce.console.getTabLink(sforce.console.TabLink.PARENT_AND_CHILDREN, currentPrimaryTabId, refreshPrimaryTab);

        var refreshPrimaryTab = function refreshPrimaryTab (res) {
            console.log('tab link is: ', res);
            sforce.console.openConsoleUrl(currentPrimaryTabId, res.tabLink, true, [], [], afterRefresh);

        var currentSubTabId; // set on page load
        var currentPrimaryTabId; // set on page load

        var getPrimaryTabId = function getPrimaryTabId (result) {
            console.log('primary tab id is: ',;
            currentPrimaryTabId =;

        var getSubtabId = function getSubtabId (result) {
            console.log('sub tab id is: ',;
            currentSubTabId =;

    var afterRefresh = function afterRefresh (res) {
            console.log('after refresh, get enclosing tab');
        var getEnclosingTab = function getEnclosingTab (res) {
            console.log('enclosing tab is: ',;

        function doCloseTab() {
        var closeSubtab = function closeSubtab(result) {
            var tabId =;

    <apex:form >
        <apex:pageMessages />
        <apex:pageBlock title="Work Assignment">
            <apex:pageBlockSection Title="User Data" columns="1" >
                <apex:pageBlockTable id="UserSkillsTable" value="{!MatchingUserSkills}" var="ms" rendered="{!IF(AND(NOT(ISBLANK(MatchingUserSkillsdata)),MatchingUserSkillsdata.size > 0), true, false)}" >
            <!-- A number of columns goes here -->

                    <apex:column style="width:7%">
                        <apex:facet name="header">Assign Project</apex:facet>

            <!-- This is where i Update the Owner of the Case field -->

                        <apex:commandLink status="loadingStatus" immediate="true" action="{!Assignrecord}" oncomplete="setBeforeUnloadEvent('{!pageswitch}');" value="Assign"> 
                            <apex:param name="Assignrowid" value="{!ms.MatchingUserId}"/> 

            <apex:pageBlockButtons location="bottom">
                <apex:commandButton status="loadingStatus" action="{!AutoAssignrecord}" value="Auto Assignment" oncomplete="setBeforeUnloadEvent('{!pageswitch}');" /> 
                <apex:commandButton status="loadingStatus" onclick="doCloseTab();" value="Back" />

My Class: (Only Button Logic I have added)
public class CIAssignProjectController {

    Public Boolean pageswitch {get;set;}
    private Case cs {get;set;}
    Private case c1;

    public CIAssignProjectController (ApexPages.StandardController controller) {
        cs =(Case) controller.getRecord();
        pageswitch = false;

    // No Problem with this code, As this is getting the Owner Field updated perfectly
    public pagereference Assignrecord(){ 

        if(cu.Profile.Name == 'CI User'){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,system.label.Projectusererrormessage));
            return null;
        // Some Codes for Update runs here , No errors on this found

                update c1;
                pageswitch = true;
                    update lstTask; 
                //ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Confirm,system.label.CI_Assign_Project));
                //PageReference updatepageRef = new PageReference('/' +;
                //return updatepageRef; 

                return null; // I return Null because, If i call PageReference it opens the case detail in the Same Sub tab.
            catch(DmlException ex){
                return null;        

MY Button Code : 
(Execute javaScript)
var caseId = '{!Case.Id}'; 

var s = caseId.toString(); 
var returnMessage = sforce.apex.execute("AssignProject","checkSLABreach",{recid:s}); 

if (sforce.console.isInConsole()) { 
sforce.console.openSubtab(, '/apex/CIAssignProject?id=' + caseId, true, 'Assign Project: {!Case.CaseNumber}', null); 
else {'/apex/CIAssignProject?id=' +caseId); 

The main problem that i guess is because the afterRefresh callback function on OpenConsoleURL is called first and then the refresh happens. 
I tried to give setTimeOut(function(){afterRefresh},5000); on openConsoleUrl, But it is not working for me. 
Amit Chaudhary 8Amit Chaudhary 8
The above RefreshPrimaryTab is not working. The Case Details is not refreshed immediately. I have to manually go and click on the Refresh button on the page to get the updated value. But In my code, the refresh happens on its own, But the issue is that VF Page reopens again. That is the problem i have. 
Can you check with sforce.console.TabLink.TAB_ONLY instead of sforce.console.TabLink.PARENT_AND_CHILDREN.