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
ajaybharathajaybharath 

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, 

MY VF PAGE: 
<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');
            sforce.console.getEnclosingPrimaryTabId(getPrimaryTabId);
            console.log('getting current subtab id');
            sforce.console.getEnclosingTabId(getSubtabId);
            if(flag){
                //First find the ID of the current tab to close it
                console.log('refreshing');
                refreshTabsAfterSave();
                //setTimeout(function(){doCloseTab();},5000)
            }
        }
        
        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: ', result.id);
            currentPrimaryTabId = result.id;
        }

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

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

        function doCloseTab() {
            sforce.console.getEnclosingTabId(closeSubtab);
        }      
        var closeSubtab = function closeSubtab(result) {
            var tabId = result.id;
            sforce.console.closeTab(tabId);
        };
        
    </script>

    <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:commandLink>
                    </apex:column>

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

</apex:page>
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;
        }
        else{
            System.debug('//////////'+lstTask);
        // Some Codes for Update runs here , No errors on this found

            try{
                update c1;
                pageswitch = true;
                if(lstTask.size()>0){
                    update lstTask; 
                }
                
                //ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Confirm,system.label.CI_Assign_Project));
                //PageReference updatepageRef = new PageReference('/' + cs.id);
                //updatepageRef.setRedirect(true);
                //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){
                ApexPages.addMessages(ex);
                return null;        
            }          
        }
    }

}
MY Button Code : 
(Execute javaScript)
{!REQUIRESCRIPT("/soap/ajax/31.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")} 
{!REQUIRESCRIPT("/support/console/27.0/integration.js")} 
var caseId = '{!Case.Id}'; 

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

if (sforce.console.isInConsole()) { 
sforce.console.getEnclosingPrimaryTabId(function(result){ 
sforce.console.openSubtab(result.id, '/apex/CIAssignProject?id=' + caseId, true, 'Assign Project: {!Case.CaseNumber}', null); 
}); 
} 
else { 
window.open('/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
ajaybharathajaybharath
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. 
ArmouryArmoury
Can you check with sforce.console.TabLink.TAB_ONLY instead of sforce.console.TabLink.PARENT_AND_CHILDREN.